Skip to content

Commit 03a615c

Browse files
rolandszokesolkimicreb
authored andcommitted
test(store): test store creation iwht both react versions
1 parent e6a680a commit 03a615c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

__tests__/store.test.jsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React, { Component } from 'react';
2+
import { render, cleanup } from '@testing-library/react/pure';
3+
// eslint-disable-next-line import/no-unresolved
4+
import { view, store } from 'react-easy-state';
5+
6+
describe('Using an old react version', () => {
7+
afterEach(cleanup);
8+
9+
test('Using local state in a function component should not throw an error', () => {
10+
const MyComp = view(() => {
11+
const person = store({ name: 'Bob' });
12+
return <div>{person.name}</div>;
13+
});
14+
expect(() => render(<MyComp />)).not.toThrow();
15+
});
16+
17+
test('Using global state in a function component should not throw an error', () => {
18+
const person = store({ name: 'Bob' });
19+
const MyComp = view(() => {
20+
return <div>{person.name}</div>;
21+
});
22+
expect(() => render(<MyComp />)).not.toThrow();
23+
});
24+
25+
test('Using global state in a class component should not throw an error', () => {
26+
const person = store({ name: 'Bob' });
27+
const MyComp = view(
28+
class extends Component {
29+
render() {
30+
return <div>{person.name}</div>;
31+
}
32+
},
33+
);
34+
expect(() => render(<MyComp />)).not.toThrow();
35+
});
36+
37+
test('Using local state in a class component should not throw an error', () => {
38+
const MyComp = view(
39+
class extends Component {
40+
person = store({ name: 'Bob' });
41+
42+
render() {
43+
return <div>{this.person.name}</div>;
44+
}
45+
},
46+
);
47+
48+
expect(() => render(<MyComp />)).not.toThrow();
49+
});
50+
51+
test('Using local state inside a render of a class component should throw an error', () => {
52+
const MyComp = view(
53+
class extends Component {
54+
render() {
55+
const person = store({ name: 'Bob' });
56+
return <div>{person.name}</div>;
57+
}
58+
},
59+
);
60+
61+
expect(() => render(<MyComp />)).toThrow(
62+
'You cannot use state inside a render of a class component. Please create your store outside of the render function.',
63+
);
64+
});
65+
});
File renamed without changes.

0 commit comments

Comments
 (0)