NewVisitorTest() in functional_tests almost passes, but needs DRY refactor; pair of functions added to HomePageTest() in apps.dashboard.tests; home_page() FBV in .views streamlined using django templating; templating also applied & form w. csrf functionality added to to-do list in home.html
This commit is contained in:
@@ -5,6 +5,12 @@ class HomePageTest(TestCase):
|
||||
response = self.client.get('/')
|
||||
self.assertTemplateUsed(response, 'apps/dashboard/home.html')
|
||||
|
||||
def test_renders_homepage_content(self):
|
||||
def test_renders_input_form(self):
|
||||
response = self.client.get('/')
|
||||
self.assertContains(response, 'To-Do')
|
||||
self.assertContains(response, '<form method="POST">')
|
||||
self.assertContains(response, '<input name="item-text"')
|
||||
|
||||
def test_can_save_a_POST_request(self):
|
||||
response = self.client.post('/', data={'item-text': 'A new dashboard item'})
|
||||
self.assertContains(response, 'A new dashboard item')
|
||||
self.assertTemplateUsed(response, 'apps/dashboard/home.html')
|
||||
@@ -1,4 +1,9 @@
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
|
||||
def home_page(request):
|
||||
return render(request, 'apps/dashboard/home.html')
|
||||
return render(
|
||||
request,
|
||||
'apps/dashboard/home.html',
|
||||
{'new_item_text': request.POST.get('item-text', '')},
|
||||
)
|
||||
|
||||
@@ -13,6 +13,7 @@ class NewVisitorTest(unittest.TestCase):
|
||||
|
||||
def test_can_start_a_todo_list(self):
|
||||
self.browser.get("http://localhost:8000")
|
||||
|
||||
self.assertIn("To-Do", self.browser.title)
|
||||
header_text = self.browser.find_element(By.TAG_NAME, 'h1').text
|
||||
self.assertIn('To-Do', header_text)
|
||||
@@ -27,12 +28,17 @@ class NewVisitorTest(unittest.TestCase):
|
||||
|
||||
table = self.browser.find_element(By.ID, 'id-list-table')
|
||||
rows = table.find_elements(By.TAG_NAME, 'tr')
|
||||
self.assertTrue(
|
||||
any(row.text == '1: Buy peacock feathers' for row in rows),
|
||||
'New to-do item did not appear in table',
|
||||
)
|
||||
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
|
||||
|
||||
self.fail("Finish the test!")
|
||||
inputbox = self.browser.find_element(By.ID, 'id-new-item')
|
||||
inputbox.send_keys('Use peacock feathers to make a fly')
|
||||
inputbox.send_keys(Keys.ENTER)
|
||||
time.sleep(1)
|
||||
|
||||
table = self.browser.find_element(By.ID, 'id-list-table')
|
||||
rows = table.find_elements(By.TAG_NAME, 'tr')
|
||||
self.assertIn('2: Use peacock feathers to make a fly', [row.text for row in rows])
|
||||
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -4,9 +4,12 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Your To-Do List</h1>
|
||||
<input id="id-new-item" placeholder="Enter a to-do item">
|
||||
<form method="POST">
|
||||
<input name="item-text" id="id-new-item" placeholder="Enter a to-do item">
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
<table id="id-list-table">
|
||||
|
||||
<tr><td>1: {{ new_item_text }}</td></tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user