From ed3535943dd31c6124c5a97d007e005708dd3615 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 13 Jan 2023 17:27:08 -0500 Subject: [PATCH] fix tests --- package.json | 2 +- src/lib/store/api.js | 13 +++++++---- src/lib/store/api.test.js | 35 ++++++++++++++++------------- src/lib/store/genNextBattle.test.js | 6 ++--- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index a00c717..69b0bb4 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "dependencies": { "@ernane/svelte-star-rating": "^1.1.2", "@sveltejs/vite-plugin-svelte": "^2.0.2", - "@yanick/updeep-remeda": "^2.0.0", + "@yanick/updeep-remeda": "^2.1.0", "beercss": "^3.0.4", "dexie": "^3.2.2", "events": "^3.3.0", diff --git a/src/lib/store/api.js b/src/lib/store/api.js index 88bc3aa..ce70e6e 100644 --- a/src/lib/store/api.js +++ b/src/lib/store/api.js @@ -1,15 +1,18 @@ -import PouchDB from 'pouchdb-browser'; +import PouchDB from 'pouchdb'; import * as R from 'remeda'; import Dexie, { liveQuery } from 'dexie'; import { writable, derived, get } from 'svelte/store'; import { genNextBattle } from './genNextBattle.js'; import u from '@yanick/updeep-remeda'; +import pouchMem from 'pouchdb-adapter-memory'; const seedCampaign = { battles: [], }; export function genApi(options = {}) { + if (options.local) PouchDB.plugin(pouchMem); + const pouchdb = new PouchDB( 'Campaigns', options.local @@ -30,16 +33,14 @@ export function genApi(options = {}) { pouchdb .changes({ since: 'now', live: true, include_docs: true }) .on('change', (change) => { - console.log({ change }); if (change.deleted) { - campaignsCore.update(u.skip(change.id)); + campaignsCore.update(u.updateIn(change.id, u.skip)); } else { campaignsCore.update(u.updateIn(change.id, change.doc)); } }); const campaigns = derived(campaignsCore, Object.values); - campaigns.subscribe((c) => console.log(c)); const addCampaign = (name) => pouchdb.post({ @@ -86,10 +87,14 @@ export function genApi(options = {}) { pouchdb.put(campaign).catch((e) => console.error(e)); }; + const deleteCampaign = (_id) => + pouchdb.remove(get(campaigns).find(u.matches({ _id }))); + return { campaigns, activeCampaign, event: { + deleteCampaign, addCampaign, setActiveCampaign, setBattleVerdict, diff --git a/src/lib/store/api.test.js b/src/lib/store/api.test.js index 912295d..517e400 100644 --- a/src/lib/store/api.test.js +++ b/src/lib/store/api.test.js @@ -1,28 +1,33 @@ import { test, expect } from 'vitest'; -//import { indexedDB, IDBKeyRange } from 'fake-indexeddb'; -import 'fake-indexeddb/auto'; +import { get } from 'svelte/store'; import { genApi } from './api.js'; -const get = async (store) => new Promise((resolve) => store.subscribe(resolve)); +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(); //{ dexie: { indexedDB, IDBKeyRange } }); + const api = genApi({ local: true }); - api.event.addCampaign('C1'); - api.event.addCampaign('C2'); + let result = waitUntil(api.campaigns, (r) => r.length === 2); - let result = await get(api.campaigns); + api.event.addCampaign('C1'); + api.event.addCampaign('C2'); - expect(result).toHaveLength(2); - expect(result[0]).toEqual({ - id: 1, - name: 'C1', - }); + await result; - api.event.deleteCampaign(1); + const [c] = get(api.campaigns); - result = await get(api.campaigns); + expect(c).toMatchObject({ + name: 'C1', + }); - expect(result).toHaveLength(1); + const r = waitUntil(api.campaigns, (r) => r.length === 1); + + api.event.deleteCampaign(c._id); + + await expect(r).resolves.toBeTruthy(); }); diff --git a/src/lib/store/genNextBattle.test.js b/src/lib/store/genNextBattle.test.js index 3e76635..d2416cb 100644 --- a/src/lib/store/genNextBattle.test.js +++ b/src/lib/store/genNextBattle.test.js @@ -3,7 +3,7 @@ import { test, expect } from 'vitest'; import { genNextBattle } from './genNextBattle.js'; test('generate for the first chapter', () => { - const next = genNextBattle(); - expect(next).toHaveProperty('city'); - expect(next).toHaveProperty('status', 'upcoming'); + const next = genNextBattle(); + expect(next).toHaveProperty('city'); + expect(next).toHaveProperty('status', 'ongoing'); });