Skip to content

Commit

Permalink
feat(signal-store): improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mini-rx committed Nov 17, 2023
1 parent 4e0d287 commit d12bf4c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 48 deletions.
26 changes: 13 additions & 13 deletions libs/common/src/lib/actions-on-queue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ describe('ActionsOnQueue', () => {
actionsOnQueue.dispatch(action);

expect(spy).toHaveBeenCalledWith(action);
}),
it('should queue actions', () => {
// Without queueScheduler this test would fail because of stack overflow (Read more here: https://blog.cloudboost.io/so-how-does-rx-js-queuescheduler-actually-work-188c1b46526e)
});
it('should queue actions', () => {
// Without queueScheduler this test would fail because of stack overflow (Read more here: https://blog.cloudboost.io/so-how-does-rx-js-queuescheduler-actually-work-188c1b46526e)

const callLimit = 5000;
const callLimit = 5000;

const actionsOnQueue = createActionsOnQueue();
const actionsOnQueue = createActionsOnQueue();

const spy = jest.fn().mockImplementation(() => {
// Every received action dispatches another action
actionsOnQueue.dispatch({ type: 'someAction' });
});
const spy = jest.fn().mockImplementation(() => {
// Every received action dispatches another action
actionsOnQueue.dispatch({ type: 'someAction' });
});

actionsOnQueue.actions$.pipe(take(callLimit)).subscribe(spy);
actionsOnQueue.actions$.pipe(take(callLimit)).subscribe(spy);

actionsOnQueue.dispatch({ type: 'someInitialAction' }); // Dispatch an action to start the whole thing
actionsOnQueue.dispatch({ type: 'someInitialAction' }); // Dispatch an action to start the whole thing

expect(spy).toHaveBeenCalledTimes(callLimit);
});
expect(spy).toHaveBeenCalledTimes(callLimit);
});
});
10 changes: 5 additions & 5 deletions libs/common/src/lib/beautify-action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('beautifyAction', () => {
type: '@mini-rx/todos/set-state',
payload: {},
});
}),
it('should not touch other normal Actions', () => {
const action = { type: 'someAction' };
});
it('should not touch other normal Actions', () => {
const action = { type: 'someAction' };

expect(beautifyAction(action)).toBe(action);
});
expect(beautifyAction(action)).toBe(action);
});
});
26 changes: 15 additions & 11 deletions libs/common/src/lib/create-rx-effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ describe('createRxEffect', () => {
expect(effect['@mini-rx/effectMetaData']).toEqual(
expect.objectContaining({ dispatch: true })
);
}),
it('should be possible to create a non-dispatching effect', () => {
const actionStream$: Observable<Action> = of({ type: 'someAction' });
const effect = createRxEffect(actionStream$, {
dispatch: false,
});

expect(effect['@mini-rx/effectMetaData']).toEqual(
expect.objectContaining({ dispatch: false })
);
});
it('should be possible to create a non-dispatching effect', () => {
const actionStream$: Observable<Action> = of({ type: 'someAction' });
const effect = createRxEffect(actionStream$, {
dispatch: false,
});

expect(effect['@mini-rx/effectMetaData']).toEqual(
expect.objectContaining({ dispatch: false })
);
});
it('should be possible to create a non-dispatching effect returning a non-action', () => {
const effect = createRxEffect(of('foo'), {
dispatch: false,
Expand All @@ -33,7 +33,11 @@ describe('createRxEffect', () => {
});

describe('hasEffectMetaData', () => {
it('should detect ', () => {
it('should detect meta data', () => {
expect(hasEffectMetaData(createRxEffect(of({ type: 'someAction' })))).toBe(true);
});

it('should NOT detect meta data', () => {
expect(hasEffectMetaData(of({ type: 'someAction' }))).toBe(false);
});
});
41 changes: 22 additions & 19 deletions libs/common/src/lib/deep-freeze.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@ describe('deepFreeze', () => {
deepFreeze(obj2);
expect(Object.isFrozen(obj2.nestedCounter)).toBe(true);
expect(() => (obj2.nestedCounter.count = 2)).toThrow();
}),
it('should freeze a function object', () => {
const fn = (): number => {
return 42;
};
expect(() => deepFreeze(fn)).not.toThrow();
// Test that the function object is frozen
expect(Object.isFrozen(fn)).toBe(true);
}),
it('should handle circular references', () => {
const circularObj: {
prop1: number;
circularRef: any;
} = { prop1: 42, circularRef: undefined };
circularObj.circularRef = circularObj;
expect(() => deepFreeze(circularObj)).not.toThrow();
// Ensure circular references do not result in infinite loops
expect(Object.isFrozen(circularObj)).toBe(true);
});
});

it('should freeze a function object', () => {
const fn = (): number => {
return 42;
};
expect(() => deepFreeze(fn)).not.toThrow();
// Test that the function object is frozen
expect(Object.isFrozen(fn)).toBe(true);
});

it('should handle circular references', () => {
const circularObj: {
prop1: number;
circularRef: any;
} = { prop1: 42, circularRef: undefined };
circularObj.circularRef = circularObj;
expect(() => deepFreeze(circularObj)).not.toThrow();
// Ensure circular references do not result in infinite loops
expect(Object.isFrozen(circularObj)).toBe(true);
});

// Arrays
it('should freeze an array and its elements', () => {
const arr = [1, 2, 3];
Expand Down

0 comments on commit d12bf4c

Please sign in to comment.