Adding a test for the new site button
Posted on Tue 07 July 2020 in conveyor
When refactoring the new flow for creating a site the + New Site button was removed so there was only one option to add a site. If you already have a site with Conveyor we need to make sure the add site button is available.
To start I have written two tests. One to make sure the button is not there if you do not have any sites and another to verify the button does exist.
def test_add_site_button_hidden_if_no_sites(self):
# this makes sure there is only one option to add a site
login_user(self.client, email=self.user.email, password='password')
sites = Site.query.all()
for site in sites:
db.session.delete(site)
db.session.commit()
response = self.client.get("/sites/")
self.assertNotIn("New Site", str(response.get_data()))
def test_add_site_button_exists_on_index(self):
login_user(self.client, email=self.user.email, password='password')
response = self.client.get("/sites/")
self.assertIn("New Site", str(response.get_data()))
While this test works right now, what if text later gets added to the page about adding a "New Site"? The tests would break. An easy solution seemed to be to add an id to the button element.
In the template I've added the following:
- A check for sites existing
- If a site exists, add the New Site button which now has an
id of
add-new-site-btn
{% if sites %}
<a id="add-new-site-btn" class="flex items-center no-underline py-2 px-4 btn-purple-full font-semibold" href="{{site_create_url}}">
<span class="text-2xl">+ </span><span>New Site</span>
</a>
{% endif %}
Now with updated tests to check for the add-new-site-btn
element:
def test_add_site_button_hidden_if_no_sites(self):
# this makes sure there is only one option to add a site
login_user(self.client, email=self.user.email, password='password')
sites = Site.query.all()
for site in sites:
db.session.delete(site)
db.session.commit()
response = self.client.get("/sites/")
self.assertNotIn('<a id="add-new-site-btn"', str(response.get_data()))
def test_add_site_button_exists_on_index(self):
login_user(self.client, email=self.user.email, password='password')
response = self.client.get("/sites/")
self.assertIn('<a id="add-new-site-btn"', str(response.get_data()))
How do you think this can be better tested? Let me know by sending me a message on Twitter @mikeabrahamsen