# Functional tests and user story for a simple "hello world" 
# to-do app created with Django

from .base import FunctionalTest
from unittest import skip

class ItemValidationTest(FunctionalTest):

    def get_error_element(self):
        return self.browser.find_element_by_css_selector('.has-error')

    def test_cannot_add_empty_quotes(self):

        # Edith goes to the home page and accidentally tries to submit an
        # empty quote. She hits enter on the empty input box
        self.browser.get(self.server_url)
        self.get_item_input_box().send_keys('\n')

        # The home page refreshes, and there is an error message saying
        # that list items cannot be left blank
        error = self.get_error_element()
        self.assertEqual(error.text, "A wise man once said nothing at all. I'll go ahead and not add that to the list...")

        # She tries again with some text for the item, which now works
        self.get_item_input_box().send_keys('LET MY PEOPLE GO!\n')
        self.check_for_row_in_list_table('1: LET MY PEOPLE GO!')

        # Perversely, she now decides to submit a second blank list item
        self.get_item_input_box().send_keys('\n')

        # She receives a similar warning on the list page
        self.check_for_row_in_list_table('1: LET MY PEOPLE GO!')
        error = self.get_error_element()
        self.assertEqual(error.text, "A wise man once said nothing at all. I'll go ahead and not add that to the list...")

        # And she can correct it by filling some text in
        self.get_item_input_box().send_keys('ADRIAN!\n')
        self.check_for_row_in_list_table('1: LET MY PEOPLE GO!')
        self.check_for_row_in_list_table('2: ADRIAN!')

    def test_cannot_add_duplicate_quotes(self):
        # Hank goes to the home page and starts a new list
        self.browser.get(self.server_url)
        self.get_item_input_box().send_keys('They speak English in What?\n')
        self.check_for_row_in_list_table('1: They speak English in What?')

        # He accidentally tries to enter a duplicate item
        self.get_item_input_box().send_keys('They speak English in What?\n')

        # He sees a helpful error message
        self.check_for_row_in_list_table('1: They speak English in What?')
        error = self.get_error_element()
        self.assertEqual(error.text, "You've already got this in your list")

    def test_error_messages_are_cleared_on_input(self):
        # Carrie starts a new list in a way that causes a validation error: 
        self.browser.get(self.server_url)
        self.get_item_input_box().send_keys('\n')
        error = self.get_error_element()
        self.assertTrue(error.is_displayed())

        #She starts typing in the input box to clear the error
        self.get_item_input_box().send_keys('a')

        #She is pleased to see that the error message disappears
        error = self.get_error_element()
        self.assertFalse(error.is_displayed())


