Merge branch 'fix-tests'

This commit is contained in:
Yanick Champoux 2023-01-13 17:27:21 -05:00
commit 59d4856904
4 changed files with 33 additions and 23 deletions

View File

@ -33,7 +33,7 @@
"dependencies": { "dependencies": {
"@ernane/svelte-star-rating": "^1.1.2", "@ernane/svelte-star-rating": "^1.1.2",
"@sveltejs/vite-plugin-svelte": "^2.0.2", "@sveltejs/vite-plugin-svelte": "^2.0.2",
"@yanick/updeep-remeda": "^2.0.0", "@yanick/updeep-remeda": "^2.1.0",
"beercss": "^3.0.4", "beercss": "^3.0.4",
"dexie": "^3.2.2", "dexie": "^3.2.2",
"events": "^3.3.0", "events": "^3.3.0",

View File

@ -1,15 +1,18 @@
import PouchDB from 'pouchdb-browser'; import PouchDB from 'pouchdb';
import * as R from 'remeda'; import * as R from 'remeda';
import Dexie, { liveQuery } from 'dexie'; import Dexie, { liveQuery } from 'dexie';
import { writable, derived, get } from 'svelte/store'; import { writable, derived, get } from 'svelte/store';
import { genNextBattle } from './genNextBattle.js'; import { genNextBattle } from './genNextBattle.js';
import u from '@yanick/updeep-remeda'; import u from '@yanick/updeep-remeda';
import pouchMem from 'pouchdb-adapter-memory';
const seedCampaign = { const seedCampaign = {
battles: [], battles: [],
}; };
export function genApi(options = {}) { export function genApi(options = {}) {
if (options.local) PouchDB.plugin(pouchMem);
const pouchdb = new PouchDB( const pouchdb = new PouchDB(
'Campaigns', 'Campaigns',
options.local options.local
@ -30,16 +33,14 @@ export function genApi(options = {}) {
pouchdb pouchdb
.changes({ since: 'now', live: true, include_docs: true }) .changes({ since: 'now', live: true, include_docs: true })
.on('change', (change) => { .on('change', (change) => {
console.log({ change });
if (change.deleted) { if (change.deleted) {
campaignsCore.update(u.skip(change.id)); campaignsCore.update(u.updateIn(change.id, u.skip));
} else { } else {
campaignsCore.update(u.updateIn(change.id, change.doc)); campaignsCore.update(u.updateIn(change.id, change.doc));
} }
}); });
const campaigns = derived(campaignsCore, Object.values); const campaigns = derived(campaignsCore, Object.values);
campaigns.subscribe((c) => console.log(c));
const addCampaign = (name) => const addCampaign = (name) =>
pouchdb.post({ pouchdb.post({
@ -86,10 +87,14 @@ export function genApi(options = {}) {
pouchdb.put(campaign).catch((e) => console.error(e)); pouchdb.put(campaign).catch((e) => console.error(e));
}; };
const deleteCampaign = (_id) =>
pouchdb.remove(get(campaigns).find(u.matches({ _id })));
return { return {
campaigns, campaigns,
activeCampaign, activeCampaign,
event: { event: {
deleteCampaign,
addCampaign, addCampaign,
setActiveCampaign, setActiveCampaign,
setBattleVerdict, setBattleVerdict,

View File

@ -1,28 +1,33 @@
import { test, expect } from 'vitest'; import { test, expect } from 'vitest';
//import { indexedDB, IDBKeyRange } from 'fake-indexeddb'; import { get } from 'svelte/store';
import 'fake-indexeddb/auto';
import { genApi } from './api.js'; 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 () => { test('create and add and remove campaigns', async () => {
const api = genApi(); //{ dexie: { indexedDB, IDBKeyRange } }); const api = genApi({ local: true });
api.event.addCampaign('C1'); let result = waitUntil(api.campaigns, (r) => r.length === 2);
api.event.addCampaign('C2');
let result = await get(api.campaigns); api.event.addCampaign('C1');
api.event.addCampaign('C2');
expect(result).toHaveLength(2); await result;
expect(result[0]).toEqual({
id: 1,
name: 'C1',
});
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();
}); });

View File

@ -3,7 +3,7 @@ import { test, expect } from 'vitest';
import { genNextBattle } from './genNextBattle.js'; import { genNextBattle } from './genNextBattle.js';
test('generate for the first chapter', () => { test('generate for the first chapter', () => {
const next = genNextBattle(); const next = genNextBattle();
expect(next).toHaveProperty('city'); expect(next).toHaveProperty('city');
expect(next).toHaveProperty('status', 'upcoming'); expect(next).toHaveProperty('status', 'ongoing');
}); });