Skip to content

Commit

Permalink
add tests for syncedCrud being able to return observables from list s…
Browse files Browse the repository at this point in the history
…o that other observables can filter a subset and have the create/update functions, tests for that in progress
  • Loading branch information
jmeistrich committed Jun 18, 2024
1 parent 3df53e8 commit 2a83344
Showing 1 changed file with 157 additions and 1 deletion.
158 changes: 157 additions & 1 deletion tests/crud.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { observable, observe, syncState, when } from '@legendapp/state';
import { configureObservableSync } from '@legendapp/state/sync';
import { syncedCrud } from '@legendapp/state/sync-plugins/crud';
import { clone } from '../src/globals';
import {
BasicValue,
BasicValue2,
Expand All @@ -10,6 +9,7 @@ import {
localStorage,
promiseTimeout,
} from './testglobals';
import { clone, getNode } from '../src/globals';

Check failure on line 12 in tests/crud.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'getNode' is defined but never used

const ItemBasicValue: () => BasicValue = () => ({
id: 'id1',
Expand Down Expand Up @@ -2061,3 +2061,159 @@ describe('Order of get/create', () => {
expect(didGet).toEqual(true);
});
});
describe('Hierarchical sync', () => {
test('list can return observables', async () => {
const obs$ = observable<Record<string, BasicValue>>(
syncedCrud({
list: () => {
return [
{ id: 'id1', test: 'hello' },
{ id: 'id2', test: 'hi' },
{ id: 'id3', test: 'hey' },
];
},
as: 'object',
}),
);

const odds$ = observable<Record<string, BasicValue>>(
syncedCrud({
list: () => {
return [obs$.id1, obs$.id3];
},
as: 'object',
}),
);

let latestValue = '';
observe(() => {
latestValue = odds$.id1.test.get();
});

expect(latestValue).toEqual('hello');

expect(odds$.get()).toEqual({
id1: { id: 'id1', test: 'hello' },
id3: { id: 'id3', test: 'hey' },
});
});
test('list can return observables with target as Promise with initial', async () => {
const obs$ = observable<Record<string, BasicValue>>(
syncedCrud({
list: async () => {
await promiseTimeout(0);
return [
{ id: 'id1', test: 'hello' },
{ id: 'id2', test: 'hi' },
{ id: 'id3', test: 'hey' },
];
},
as: 'object',
}),
);

const odds$ = observable<Record<string, BasicValue>>(
syncedCrud({
initial: () => ({
id1: obs$['id1'],
id3: obs$['id3'],
}),
}),
);

let latestValue = '';
observe(() => {
latestValue = odds$.id1.test.get();
});

expect(latestValue).toEqual(undefined);

await promiseTimeout(0);

expect(latestValue).toEqual('hello');

expect(odds$.get()).toEqual({
id1: { id: 'id1', test: 'hello' },
id3: { id: 'id3', test: 'hey' },
});
});
// TODO: Would be good to get this working with list
// test('list can return observables with target as Promise with list', async () => {
// const obs$ = observable<Record<string, BasicValue>>(
// syncedCrud({
// list: async () => {
// await promiseTimeout(0);
// return [
// { id: 'id1', test: 'hello' },
// { id: 'id2', test: 'hi' },
// { id: 'id3', test: 'hey' },
// ];
// },
// as: 'object',
// }),
// );

// const odds$ = observable<Record<string, BasicValue>>(
// syncedCrud({
// list: () => {
// return [obs$.id1, obs$.id3];
// },
// as: 'object',
// }),
// );

// // @ts-expect-error asdf
// getNode(odds$).___test = true;

// let latestValue = '';
// observe(() => {
// latestValue = odds$.id1.test.get();
// console.log(latestValue);
// });

// expect(latestValue).toEqual(undefined);

// await promiseTimeout(0);

// expect(latestValue).toEqual('hello');

// expect(odds$.get()).toEqual({
// id1: { id: 'id1', test: 'hello' },
// id3: { id: 'id3', test: 'hey' },
// });
// });
// test('child of a list has create', async () => {
// // TODO: The purpose of this one is to have obs$ contain the list and odds$ to locally filter a subset and have the create/update on it
// const obs$ = observable<Record<string, BasicValue>>(
// syncedCrud({
// list: () => {
// return promiseTimeout(0, [
// { id: 'id1', test: 'hello' },
// { id: 'id2', test: 'hi' },
// { id: 'id3', test: 'hey' },
// ]);
// },
// as: 'object',
// }),
// );

// const odds$ = observable<Record<string, BasicValue>>(
// syncedCrud({
// list: () => {
// obs$.get();
// return [obs$.id1, obs$.id3];
// },
// as: 'object',
// }),
// );

// odds$.get();

// await promiseTimeout(10);

// expect(odds$.get()).toEqual({
// id1: { id: 'id1', test: 'hello' },
// id3: { id: 'id3', test: 'hey' },
// });
// });
});

0 comments on commit 2a83344

Please sign in to comment.