ECOSYSTEM / STORE

Testing stores

Test a store with plain Python, and check what a view did to it in a headless test.

Ecosystem package janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-store
STORE · PAGE 7 / 8~1 min read
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

The logic, without Qt

A loose instance, Store(), needs no application: call its actions and check its fields with plain asserts, as in the examples of Controlled writes and Tracking changes. Keep most tests there; they are fast and point at the exact rule that broke.

What a view did to the store

When imported, the package adds two methods to janelkit.inspect: app.store(Class, key=None) returns the instance of the mounted application, and app.mutations() its mutations, from the oldest to the newest. Each mount starts with fresh instances and an empty temporary data folder.

python
# tests/test_cart.py, for the example in Overview.
from janelkit.inspect import mount
from stores.cart import Cart

def test_add_an_apple():
    with mount("src/App.janel") as app:
        app.find("button", text="Add an apple").click()
        assert len(app.store(Cart).items) == 1
        assert [m.action for m in app.mutations()] == ["add"]
Copy

How it works

  1. The click goes through the real button, like a user.
  2. app.store(Cart) is the same instance the views use.
  3. The list of actions shows that the view changed the store through add, not directly.

Examples

apps/examples/carrito, in the JanelKit repository, shares one strict store between two windows and logs every change. apps/examples/preferencias remembers preferences with persisted.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.