Breaking changes in how to test components, for better
Using async with reactor() as comm
, you get an object that you can use to register components, send commands... doc
is a PyQuery object containing the HTML rendering of the object, so you can perform checkings of the rendering of the object.
from pytest import mark
from channels.db import database_sync_to_async as db
from reactor.tests import reactor
@mark.django_db(transaction=True)
async def test_live_components():
x_list = comm.add_component(
'x-todo-list',
{'id': 'someid', 'showing': 'all'}
)
x_todo_counter = comm.add_component(
'x-todo-counter',
{'id': 'someid-counter'}
)
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
doc = await comm.send(x_list, 'add', new_item='First task')
# There was an item crated and rendered
assert await db(Item.objects.count)() == 1
assert len(doc('x-todo-item')) == 1
todo_item_id = doc('x-todo-item')[0].get('id')
todo_item_label = doc('x-todo-item label')[0]
assert todo_item_label.text == 'First task'