Skip to content

Commit 1628120

Browse files
committed
test: improve tests for withViewModel HOC
1 parent 6b0c9b5 commit 1628120

File tree

7 files changed

+103
-47
lines changed

7 files changed

+103
-47
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ jsconfig.json
1616
*.json
1717
.eslintrc.cjs
1818
post-build.mjs
19-
vitest.config.ts
19+
vitest.config.ts
20+
setupTests.js

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"react": ">=18.3.1"
4141
},
4242
"devDependencies": {
43+
"@testing-library/jest-dom": "6.6.3",
4344
"@testing-library/react": "16.0.1",
4445
"@types/lodash-es": "^4.17.12",
4546
"@types/node": "20.14.5",

pnpm-lock.yaml

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

setupTests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { expect } from 'vitest';
2+
import * as matchers from '@testing-library/jest-dom/matchers';
3+
4+
expect.extend(matchers);

src/hoc/with-view-model.test.tsx

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,79 @@
1-
import { act, render, screen, } from '@testing-library/react';
1+
import { act, render, screen } from '@testing-library/react';
22
import { observer } from 'mobx-react-lite';
33
import { ReactNode } from 'react';
44
import { describe, expect, test } from 'vitest';
55

66
import { ViewModelsProvider } from '..';
7+
import { createCounter } from '../utils';
78
import { TestViewModelStoreImpl } from '../view-model/abstract-view-model.store.test';
89
import { TestViewModelImpl } from '../view-model/view-model.impl.test';
910

1011
import { ViewModelProps, withViewModel } from './with-view-model';
11-
import { createCounter } from '../utils';
1212

1313
const createIdGenerator = (prefix?: string) => {
1414
const counter = createCounter();
15-
return () => (prefix || '') + counter().toString()
16-
}
15+
return () => (prefix ?? '') + counter().toString();
16+
};
1717

1818
describe('withViewModel', () => {
1919
test('renders', () => {
20-
class VM extends TestViewModelImpl { }
20+
class VM extends TestViewModelImpl {}
2121
const View = ({ model }: ViewModelProps<VM>) => {
2222
return <div>{`hello ${model.id}`}</div>;
2323
};
24-
const Component = withViewModel(VM, { generateId: createIdGenerator() })(View);
24+
const Component = withViewModel(VM, { generateId: createIdGenerator() })(
25+
View,
26+
);
2527

2628
render(<Component />);
2729
expect(screen.getByText('hello VM_0')).toBeDefined();
2830
});
2931

3032
test('renders nesting', () => {
31-
class VM1 extends TestViewModelImpl { }
32-
const View1 = ({
33+
const Component1 = withViewModel(TestViewModelImpl)(({
3334
children,
34-
}: ViewModelProps<VM1> & { children?: ReactNode }) => {
35+
}: {
36+
children?: ReactNode;
37+
}) => {
3538
return (
36-
<div data-testid="parent-container">
39+
<div data-testid={'parent-container'}>
3740
<div>parent</div>
38-
<div>{children}</div>
41+
{children}
3942
</div>
4043
);
41-
};
42-
const Component1 = withViewModel(VM1, { generateId: createIdGenerator() })(View1);
43-
44-
const Component2 = withViewModel(class VM2 extends TestViewModelImpl { }, { generateId: createIdGenerator() })(
45-
() => {
46-
return <div>child</div>;
47-
},
48-
49-
);
44+
});
45+
const Component2 = withViewModel(TestViewModelImpl)(() => {
46+
return <div>child</div>;
47+
});
5048

51-
render(
49+
const { container } = render(
5250
<Component1>
5351
<Component2 />
5452
</Component1>,
5553
);
56-
expect(screen.getByTestId('parent-container')).toBe(`<div
57-
data-testid="parent-container"
58-
>
59-
<div>
60-
parent
61-
</div>
62-
<div>
63-
<div>
64-
child
65-
</div>
66-
</div>
67-
</div>`);
54+
55+
expect(container.firstChild).toMatchInlineSnapshot(`
56+
<div
57+
data-testid="parent-container"
58+
>
59+
<div>
60+
parent
61+
</div>
62+
<div>
63+
child
64+
</div>
65+
</div>
66+
`);
6867
});
6968

7069
test('renders twice', async () => {
71-
class VM extends TestViewModelImpl { }
70+
class VM extends TestViewModelImpl {}
7271
const View = ({ model }: ViewModelProps<VM>) => {
7372
return <div>{`hello ${model.id}`}</div>;
7473
};
75-
const Component = withViewModel(VM, { generateId: createIdGenerator() })(View);
74+
const Component = withViewModel(VM, { generateId: createIdGenerator() })(
75+
View,
76+
);
7677

7778
render(
7879
<>
@@ -85,7 +86,7 @@ describe('withViewModel', () => {
8586
});
8687

8788
test('renders with fixed id', () => {
88-
class VM extends TestViewModelImpl { }
89+
class VM extends TestViewModelImpl {}
8990
const View = ({ model }: ViewModelProps<VM>) => {
9091
return <div>{`hello ${model.id}`}</div>;
9192
};
@@ -96,7 +97,7 @@ describe('withViewModel', () => {
9697
});
9798

9899
test('renders twice with fixed id', async () => {
99-
class VM extends TestViewModelImpl { }
100+
class VM extends TestViewModelImpl {}
100101
const View = ({ model }: ViewModelProps<VM>) => {
101102
return <div>{`hello ${model.id}`}</div>;
102103
};
@@ -112,15 +113,15 @@ describe('withViewModel', () => {
112113
});
113114

114115
test('renders with view model store', async () => {
115-
class VM extends TestViewModelImpl { }
116+
class VM extends TestViewModelImpl {}
116117
const View = observer(({ model }: ViewModelProps<VM>) => {
117118
return (
118119
<div>
119-
<div>{`hello my friend. Model has id ? ${!!model.id}`}</div>
120+
<div>{`hello my friend. Model id is ${model.id}`}</div>
120121
</div>
121122
);
122123
});
123-
const Component = withViewModel(VM)(View);
124+
const Component = withViewModel(VM, { generateId: () => '1' })(View);
124125
const vmStore = new TestViewModelStoreImpl();
125126

126127
const Wrapper = ({ children }: { children?: ReactNode }) => {
@@ -135,8 +136,6 @@ describe('withViewModel', () => {
135136
}),
136137
);
137138

138-
expect(
139-
screen.getByText('hello my friend. Model has id ? true'),
140-
).toBeDefined();
139+
expect(screen.getByText('hello my friend. Model id is VM_1')).toBeDefined();
141140
});
142141
});

src/hoc/with-view-model.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export function withViewModel(
101101
if (!idRef.current) {
102102
idRef.current =
103103
viewModels?.generateViewModelId({
104-
ctx: ctx,
105-
id: config?.id,
104+
ctx,
105+
id: config?.id,
106106
VM: Model,
107107
parentViewModelId,
108108
fallback: config?.fallback,
@@ -124,7 +124,7 @@ export function withViewModel(
124124
(parentViewModelId && instances.get(parentViewModelId)) || null,
125125
fallback: config?.fallback,
126126
instances,
127-
ctx: ctx,
127+
ctx,
128128
};
129129

130130
const instance =

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export default defineConfig({
77
test: {
88
globals: true,
99
environment: "jsdom",
10+
setupFiles: './setupTests.js',
1011
},
1112
});

0 commit comments

Comments
 (0)