Merge branch 'fix-tests'
This commit is contained in:
commit
59d4856904
@ -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",
|
||||
|
@ -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,
|
||||
|
@ -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 });
|
||||
|
||||
let result = waitUntil(api.campaigns, (r) => r.length === 2);
|
||||
|
||||
api.event.addCampaign('C1');
|
||||
api.event.addCampaign('C2');
|
||||
|
||||
let result = await get(api.campaigns);
|
||||
await result;
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0]).toEqual({
|
||||
id: 1,
|
||||
const [c] = get(api.campaigns);
|
||||
|
||||
expect(c).toMatchObject({
|
||||
name: 'C1',
|
||||
});
|
||||
|
||||
api.event.deleteCampaign(1);
|
||||
const r = waitUntil(api.campaigns, (r) => r.length === 1);
|
||||
|
||||
result = await get(api.campaigns);
|
||||
api.event.deleteCampaign(c._id);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
await expect(r).resolves.toBeTruthy();
|
||||
});
|
||||
|
@ -5,5 +5,5 @@ import { genNextBattle } from './genNextBattle.js';
|
||||
test('generate for the first chapter', () => {
|
||||
const next = genNextBattle();
|
||||
expect(next).toHaveProperty('city');
|
||||
expect(next).toHaveProperty('status', 'upcoming');
|
||||
expect(next).toHaveProperty('status', 'ongoing');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user