import { test, expect } from 'vitest'; import { get } from 'svelte/store'; import { genApi } from './api.js'; import adapter from 'pouchdb-adapter-memory'; const waitUntil = (store, condition) => { return new Promise((resolve) => { store.subscribe((v) => condition(v) && resolve(v)); }); }; test('create and add and remove campaigns', async () => { const api = genApi({ local: true, pouch: (p) => p.plugin(adapter), }); let result = waitUntil(api.campaigns, (r) => r.length === 2); api.event.addCampaign('C1'); api.event.addCampaign('C2'); await result; const [c] = get(api.campaigns); expect(c).toMatchObject({ name: 'C1', }); const r = waitUntil(api.campaigns, (r) => r.length === 1); api.event.deleteCampaign(c._id); await expect(r).resolves.toBeTruthy(); });