Skip to content

Commit

Permalink
test: Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mnasyrov committed Jan 11, 2022
1 parent 931b949 commit bb0a9c0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/rx-effects/src/stateDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,31 @@ describe('StateDeclaration', () => {
value: 2,
});
});

it('should set a name to the store', () => {
const declaration: StateDeclaration<State> = declareState(stateFactory, {
name: 'foo',
});

const store = declaration.createStore();
expect(store.name).toBe('foo');
});

it('should use a provided comparator for the store', () => {
const stateComparator = (a: State, b: State) => a.value === b.value;

const declaration: StateDeclaration<State> = declareState(stateFactory, {
stateComparator,
});

const store = declaration.createStore();
expect(store.get().data).toBe(undefined);

store.set({ value: 1, data: 'a' });
expect(store.get().data).toBe(undefined);

store.set({ value: 2, data: 'b' });
expect(store.get().data).toBe('b');
});
});
});
19 changes: 19 additions & 0 deletions packages/rx-effects/src/storeActions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { declareState } from './stateDeclaration';
import { createStore } from './store';
import { createStoreActions } from './storeActions';

Expand All @@ -16,4 +17,22 @@ describe('StoreActions', () => {
storeActions.multiply(3);
expect(store.get()).toBe(9);
});

it('should return a factory in case a store is not specified', () => {
const declaration = declareState(1);

const getStoreActions = declaration.createStoreActions({
add: (value: number) => (state: number) => state + value,
multiply: (value: number) => (state: number) => state * value,
});

const store = declaration.createStore();
const storeActions = getStoreActions(store);

storeActions.add(2);
expect(store.get()).toBe(3);

storeActions.multiply(3);
expect(store.get()).toBe(9);
});
});
25 changes: 25 additions & 0 deletions packages/rx-effects/src/storeExtensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createStore } from './store';
import { registerStoreExtension } from './storeExtensions';

describe('registerStoreExtension()', () => {
it('should register an extension', () => {
const eventHandler = jest.fn();

registerStoreExtension(() => ({
onStoreEvent: eventHandler,
}));

createStore<number>(0, { name: 'test' });

expect(eventHandler).nthCalledWith(1, {
type: 'created',
store: expect.objectContaining({ name: 'test' }),
});
});

it('should register an empty extension', () => {
registerStoreExtension(() => ({}));
createStore<number>(0, { name: 'test' });
expect.assertions(0);
});
});
9 changes: 9 additions & 0 deletions packages/rx-effects/src/storeLoggerExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ describe('createStoreLoggerExtension()', () => {

expect(logger).nthCalledWith(8, 'test#1', 'destroyed');
});

it('should log only store ID in case its name is empty', () => {
const logger = jest.fn();

registerStoreExtension(createStoreLoggerExtension(logger));

createStore<number>(0);
expect(logger).nthCalledWith(1, expect.stringMatching(/^#\d/), 'created');
});
});
2 changes: 1 addition & 1 deletion packages/rx-effects/src/storeLoggerExtension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StoreExtension } from './storeExtensions';

export function createStoreLoggerExtension(
logger: typeof console.log = console.log,
logger: typeof console.log,
): StoreExtension<unknown> {
return (api) => ({
onStoreEvent(event) {
Expand Down

0 comments on commit bb0a9c0

Please sign in to comment.