Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ComposedStore types to account for primitives #81

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ComposedStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ObservableStore } from './ObservableStore';

export class ComposedStore<
T extends Record<string, Record<string, unknown>>,
T extends Record<string, unknown>,
> extends ObservableStore<T> {
private _children: Record<keyof T, ObservableStore<T[keyof T]>>;

Expand Down
40 changes: 29 additions & 11 deletions test/composed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'tape';
import { ComposedStore, ObservableStore } from '../src';

test('ComposedStore - basic', function (t) {
test('ComposedStore - unknown state', function (t) {
t.plan(1);

// @ts-expect-error The signature for the ObservableStore constructor makes it
Expand All @@ -11,11 +11,7 @@ test('ComposedStore - basic', function (t) {
// seem like an argument is required, but we're not passing an argument here.
const childStoreTwo = new ObservableStore();
const composedStore = new ComposedStore({
// @ts-expect-error TypeScript produces an error because ComposedStore
// isn't typed correctly.
one: childStoreOne,
// @ts-expect-error TypeScript produces an error because ComposedStore
// isn't typed correctly.
two: childStoreTwo,
});

Expand All @@ -29,23 +25,45 @@ test('ComposedStore - basic', function (t) {
);
});

test('ComposedStore - child initState', function (t) {
test('ComposedStore - primitive state', function (t) {
t.plan(1);

const childStoreOne = new ObservableStore(1);
const childStoreTwo = new ObservableStore(2);
const composedStore = new ComposedStore({
// @ts-expect-error TypeScript produces an error because ComposedStore
// isn't typed correctly.
one: childStoreOne,
// @ts-expect-error TypeScript produces an error because ComposedStore
// isn't typed correctly.
two: childStoreTwo,
});

childStoreOne.putState(2);
childStoreTwo.putState(4);

t.deepEqual(
composedStore.getState(),
{ one: 1, two: 2 },
{ one: 2, two: 4 },
'composedStore gets state from children',
);
});

test('ComposedStore - non-primitive state', function (t) {
t.plan(1);

const childStoreOne = new ObservableStore({ foo: 'bar' });
const childStoreTwo = new ObservableStore({ baz: 'qux' });
const composedStore = new ComposedStore({
one: childStoreOne,
two: childStoreTwo,
});

childStoreOne.putState({ foo: 'fizz' });
childStoreTwo.putState({ baz: 'buzz' });

t.deepEqual(
composedStore.getState(),
{
one: { foo: 'buzz' },
two: { baz: 'buzz' },
},
'composedStore gets state from children',
);
});