Skip to content

Commit

Permalink
feat: create use posts store initial and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmeida00 committed Aug 15, 2023
1 parent aad3865 commit 5a89466
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/stores/__mocks__/zunstandMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as zustand from 'zustand';

const actualCreate = zustand.create;
export const storeResetFns = new Set<() => void>();
export const myCustomCreate = <T>() => {
return (stateCreator: zustand.StateCreator<T>) => {
const store = actualCreate(stateCreator);
const initialState = store.getState();
storeResetFns.add(() => {
store.setState(initialState, true);
});
return store;
};
};
53 changes: 53 additions & 0 deletions src/stores/usePostsStore.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { renderHook, act } from '@testing-library/react';
import * as zustand from 'zustand';

import { usePostsStore } from './usePostsStore';

import { myCustomCreate, storeResetFns } from './__mocks__/zunstandMock';

jest.spyOn(zustand, 'create').mockImplementation(myCustomCreate as never);

describe('usePostsStore', () => {
afterEach(() => {
act(() => {
storeResetFns.forEach((resetFn) => {
resetFn();
});
});
});

describe('when initialize', () => {
it('posts should be zero', () => {
const { result } = renderHook(() => usePostsStore((state) => state));
expect(result.current.postsQuantity).toBe(0);
});
});

describe('when increased the number of posts', () => {
it('set value to tree', () => {
const { result } = renderHook(() => usePostsStore((state) => state));

act(() => {
result.current.increase(3);
});

expect(result.current.postsQuantity).toBe(3);
});
});

describe('when we reset the number of posts', () => {
it('change to tree and reset to zero', () => {
const { result } = renderHook(() => usePostsStore((state) => state));

act(() => {
result.current.increase(2);
});

act(() => {
result.current.reset();
});

expect(result.current.postsQuantity).toBe(0);
});
});
});
File renamed without changes.

0 comments on commit 5a89466

Please sign in to comment.