Skip to content

Commit b2366c7

Browse files
committed
test: add missing unit test cases
1 parent dd187e9 commit b2366c7

File tree

8 files changed

+823
-14
lines changed

8 files changed

+823
-14
lines changed

.gitignore

Lines changed: 598 additions & 4 deletions
Large diffs are not rendered by default.

jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { JestConfigWithTsJest } from 'ts-jest';
22

33
const jestConfig: JestConfigWithTsJest = {
4-
collectCoverage: false,
4+
collectCoverage: true,
55
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
66
coverageDirectory: 'coverage',
77
moduleDirectories: [

src/components/button/button.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import { renderWrapper, screen } from '@/tests';
33
import { Button } from './button';
44

55
describe('Button Component', () => {
6-
test('renders the component', () => {
6+
it('renders the component', () => {
77
renderWrapper(<Button>Hello</Button>);
88
expect(screen.getByText((content) => content.includes('Edit'))).toBeInTheDocument();
99
});
10-
test('renders the component and finds the button by text', () => {
10+
it('renders the component and finds the button by text', () => {
1111
renderWrapper(<Button>Hello</Button>);
1212
expect(screen.getByText(/edit/i)).toBeInTheDocument();
1313
});
14-
test('renders the component and finds the button by role', () => {
14+
it('renders the component and finds the button by role', () => {
1515
renderWrapper(<Button>Hello</Button>);
1616
expect(screen.getByRole('button')).toBeInTheDocument();
1717
});
1818

19-
test('renders the component and finds the button by text', () => {
19+
it('renders the component and finds the button by text', () => {
2020
renderWrapper(<Button>Hello</Button>);
2121
expect(screen.getByText((content) => content.includes('Hello'))).toBeInTheDocument();
2222
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { render } from '@testing-library/react';
2+
3+
import { Trans, TransMacro } from './trans';
4+
5+
jest.mock('@lingui/react', () => ({
6+
Trans: jest.fn(({ children, ...props }) => (
7+
<span
8+
data-testid="mock-trans"
9+
{...props}
10+
>
11+
{children}
12+
</span>
13+
)),
14+
}));
15+
16+
jest.mock('@lingui/react/macro', () => ({
17+
Trans: jest.fn(({ children, ...props }) => (
18+
<span
19+
data-testid="mock-macro"
20+
{...props}
21+
>
22+
{children}
23+
</span>
24+
)),
25+
}));
26+
27+
describe('Trans Component', () => {
28+
it('should render the Trans component with passed props', () => {
29+
const { getByTestId } = render(<Trans id="test.message" />);
30+
const element = getByTestId('mock-trans');
31+
32+
expect(element).toBeInTheDocument();
33+
expect(element).toHaveAttribute('id', 'test.message');
34+
});
35+
36+
it('should render children correctly', () => {
37+
const { getByTestId, getByText } = render(
38+
<Trans id="test.message">Hello, world!</Trans>,
39+
);
40+
const element = getByTestId('mock-trans');
41+
42+
expect(element).toBeInTheDocument();
43+
expect(getByText('Hello, world!')).toBeInTheDocument();
44+
});
45+
});
46+
47+
describe('TransMacro Component', () => {
48+
it('should render the TransMacro component with passed props', () => {
49+
const { getByTestId } = render(<TransMacro id="macro.message">Test</TransMacro>);
50+
const element = getByTestId('mock-macro');
51+
52+
expect(element).toBeInTheDocument();
53+
expect(element).toHaveAttribute('id', 'macro.message');
54+
});
55+
56+
it('should render children correctly', () => {
57+
const { getByTestId, getByText } = render(
58+
<TransMacro id="macro.message">This is a macro!</TransMacro>,
59+
);
60+
const element = getByTestId('mock-macro');
61+
62+
expect(element).toBeInTheDocument();
63+
expect(getByText('This is a macro!')).toBeInTheDocument();
64+
});
65+
});

src/hooks/trans.hook.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { msg } from '@lingui/core/macro';
2+
import { useLingui } from '@lingui/react';
3+
import { renderHook } from '@testing-library/react';
4+
5+
import { useTrans } from './trans.hook';
6+
7+
jest.mock('@lingui/react');
8+
jest.mock('@lingui/core/macro');
9+
10+
describe('useTrans hook', () => {
11+
const mockI18n = {
12+
_: jest.fn(),
13+
};
14+
15+
beforeEach(() => {
16+
(useLingui as jest.Mock).mockReturnValue({ i18n: mockI18n });
17+
});
18+
19+
it('should return i18n object', () => {
20+
const { result } = renderHook(() => useTrans());
21+
expect(result.current.i18n).toBe(mockI18n);
22+
});
23+
24+
it('should return macro function', () => {
25+
const { result } = renderHook(() => useTrans());
26+
expect(result.current.macro).toBe(msg);
27+
});
28+
29+
it('should call i18n._ with correct parameters in trans function', () => {
30+
const { result } = renderHook(() => useTrans());
31+
const id = 'test.id';
32+
const values = { name: 'John' };
33+
result.current.trans(id, values);
34+
expect(mockI18n._).toHaveBeenCalledWith(id, values);
35+
});
36+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { i18n } from '@lingui/core';
2+
3+
import { renderWrapper, waitFor } from '@/tests';
4+
5+
import { messages as enMessages } from '../locales/en/messages';
6+
import { IntlProvider } from './intl.provider';
7+
8+
jest.mock('@lingui/core', () => {
9+
const originalModule = jest.requireActual('@lingui/core');
10+
return {
11+
...originalModule,
12+
i18n: {
13+
...originalModule.i18n,
14+
activate: jest.fn(),
15+
load: jest.fn(),
16+
on: jest.fn(),
17+
},
18+
};
19+
});
20+
21+
describe('IntlProvider Component', () => {
22+
beforeAll(() => {
23+
i18n.load({ en: enMessages });
24+
i18n.activate('en');
25+
});
26+
27+
it('should activate the default locale (en) if none is provided', async () => {
28+
renderWrapper(
29+
<IntlProvider>
30+
<div>Default Locale</div>
31+
</IntlProvider>,
32+
);
33+
34+
await waitFor(() => expect(i18n.activate).toHaveBeenCalledWith('en'));
35+
});
36+
37+
it('should activate the specified locale', async () => {
38+
const locale = 'en';
39+
40+
renderWrapper(
41+
<IntlProvider locale={locale}>
42+
<div>Specific Locale</div>
43+
</IntlProvider>,
44+
);
45+
46+
await waitFor(() => expect(i18n.activate).toHaveBeenCalledWith(locale));
47+
});
48+
49+
it('should load the correct catalogs', async () => {
50+
renderWrapper(
51+
<IntlProvider locale="en">
52+
<div>Catalog Test</div>
53+
</IntlProvider>,
54+
);
55+
56+
await waitFor(() => expect(i18n.load).toHaveBeenCalledWith({ en: enMessages }));
57+
});
58+
});

src/providers/intl.provider.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { PropsWithChildren } from 'react';
55
// Import Catalogs
66
import { messages as enMessages } from '../locales/en/messages';
77

8-
// Inject Catalogs
9-
i18n.load({
10-
en: enMessages,
11-
});
12-
138
export const IntlProvider = ({ children, locale = 'en' }: PropsWithChildren<{ locale?: string }>) => {
9+
// Inject Catalogs
10+
i18n.load({
11+
en: enMessages,
12+
});
13+
1414
// Activate Catalog
1515
i18n.activate(locale);
1616

src/stores/app.store.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { act, renderHook } from '@/tests';
2+
3+
import { useAppStores } from './app.store';
4+
5+
describe('useAppStores Zustand Store', () => {
6+
it('should initialize with 0 bears', () => {
7+
const { result } = renderHook(() => useAppStores());
8+
expect(result.current.bears).toBe(0);
9+
});
10+
11+
it('should increase the bear population by 1', () => {
12+
const { result } = renderHook(() => useAppStores());
13+
14+
act(() => {
15+
result.current.increasePopulation();
16+
});
17+
18+
expect(result.current.bears).toBe(1);
19+
});
20+
21+
it('should reset the bear population to 0', () => {
22+
const { result } = renderHook(() => useAppStores());
23+
24+
act(() => {
25+
result.current.increasePopulation();
26+
result.current.increasePopulation();
27+
result.current.removeAllBears();
28+
});
29+
30+
expect(result.current.bears).toBe(0);
31+
});
32+
33+
it('should update the bear population to a specific number', () => {
34+
const { result } = renderHook(() => useAppStores());
35+
36+
act(() => {
37+
result.current.updateBears(5);
38+
});
39+
40+
expect(result.current.bears).toBe(5);
41+
});
42+
43+
it('should handle multiple updates correctly', () => {
44+
const { result } = renderHook(() => useAppStores());
45+
46+
act(() => {
47+
result.current.updateBears(10);
48+
result.current.increasePopulation();
49+
result.current.increasePopulation();
50+
result.current.removeAllBears();
51+
result.current.updateBears(7);
52+
});
53+
54+
expect(result.current.bears).toBe(7);
55+
});
56+
});

0 commit comments

Comments
 (0)