fix tests

This commit is contained in:
Yanick Champoux 2023-01-13 17:27:08 -05:00
parent 834b317b2f
commit ed3535943d
4 changed files with 33 additions and 23 deletions

View File

@ -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",

View File

@ -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,

View File

@ -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();
});

View File

@ -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');
});