Skip to content

Commit

Permalink
Even better testing API
Browse files Browse the repository at this point in the history
  • Loading branch information
edelvalle committed Jun 24, 2019
1 parent 8f56f53 commit 359777e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
6 changes: 4 additions & 2 deletions reactor/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async def send_join(self, component_id):
},
})
await self.loop_over_messages()
return component.doc

async def send(self, _id, _name, **state):
assert _id in self._components
Expand All @@ -45,9 +46,10 @@ async def send(self, _id, _name, **state):
}
})
await self.loop_over_messages()
return self._components[_id].doc

async def loop_over_messages(self):
while not await self.receive_nothing():
async def loop_over_messages(self, timeout=0.1):
while not await self.receive_nothing(timeout=timeout):
response = await self.receive_json_from()
if response['type'] in ('redirect', 'push_state'):
self.redirected_to = response['url']
Expand Down
30 changes: 10 additions & 20 deletions tests/fision/todo/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ async def test_live_components():
'x-todo-counter',
{'id': 'someid-counter'}
)
await comm.send_join(x_list)
doc = comm[x_list].doc
doc = await comm.send_join(x_list)
todo_list = doc('#someid')
assert json.loads(todo_list.attr['state']) == comm[x_list].state

# Add new item
await comm.send(x_list, 'add', new_item='First task')
doc = await comm.send(x_list, 'add', new_item='First task')

# There was an item crated and rendered
doc = comm[x_list].doc
assert await db(Item.objects.count)() == 1
assert len(doc('x-todo-item')) == 1
todo_item_id = doc('x-todo-item')[0].get('id')
Expand All @@ -65,13 +63,11 @@ async def test_live_components():
# Click title to edit it
assert len(doc('x-todo-item li.editing')) == 0
x_first_item = comm.add_component('x-todo-item', {'id': todo_item_id})
await comm.send(x_first_item, 'toggle_editing')
doc = comm[x_first_item].doc
doc = await comm.send(x_first_item, 'toggle_editing')
assert len(doc('x-todo-item li.editing')) == 1

# Edit item with a new text
await comm.send(x_first_item, 'save', text='Edited task')
doc = comm[x_first_item].doc
doc = await comm.send(x_first_item, 'save', text='Edited task')
assert len(doc('x-todo-item li.editing')) == 0, 'Not in read mode'
todo_item_label = doc('x-todo-item label')[0]
assert todo_item_label.text == 'Edited task'
Expand All @@ -84,36 +80,31 @@ async def test_live_components():

# Mark item as completed
assert len(doc('x-todo-item li.completed')) == 0
await comm.send(x_first_item, 'completed', completed=True)
doc = await comm.send(x_first_item, 'completed', completed=True)

# Item is completed
doc = comm[x_first_item].doc
assert len(doc('x-todo-item li.completed')) == 1

# Counter is rendered as 0
doc = comm[x_todo_counter].doc
assert doc('strong')[0].text == '0'

# Switch to "only active tasks" filtering
await comm.send(x_list, 'show', showing='active')
doc = comm[x_list].doc
doc = await comm.send(x_list, 'show', showing='active')
assert len(doc('x-todo-item li.hidden')) == 1

# Switch to "only done tasks" filtering
await comm.send(x_list, 'show', showing='completed')
doc = comm[x_list].doc
doc = await comm.send(x_list, 'show', showing='completed')
assert len(doc('x-todo-item')) == 1
assert len(doc('x-todo-item li.hidden')) == 0

# Switch to "all tasks" filtering
await comm.send(x_list, 'show', showing='all')
doc = comm[x_list].doc
doc = await comm.send(x_list, 'show', showing='all')
assert len(doc('x-todo-item')) == 1
assert len(doc('x-todo-item li.hidden')) == 0

# Add another task
await comm.send(x_list, 'add', new_item='Another task')
doc = comm[x_list].doc
doc = await comm.send(x_list, 'add', new_item='Another task')
assert len(doc('x-todo-item')) == 2
assert len(doc('x-todo-item li.completed')) == 1

Expand All @@ -122,8 +113,7 @@ async def test_live_components():
assert doc('strong')[0].text == '1'

# Remove completed tasks removes just one task
await comm.send(x_list, 'clear_completed')
doc = comm[x_list].doc
doc = await comm.send(x_list, 'clear_completed')
assert len(doc('x-todo-item')) == 1
assert len(doc('x-todo-item.completed')) == 0

Expand Down

0 comments on commit 359777e

Please sign in to comment.