Skip to content

Commit f912772

Browse files
committed
test(app-events-native): fix unit tests
1 parent 3a15cec commit f912772

1 file changed

Lines changed: 36 additions & 33 deletions

File tree

packages/pluggableWidgets/app-events-native/src/__tests__/AppEvents.spec.tsx

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { actionValue } from "@mendix/piw-utils-internal";
22
import { createElement } from "react";
33
import { AppStateStatus } from "react-native";
4-
import { render } from "@testing-library/react-native";
4+
5+
import { mount } from "enzyme";
56

67
import { AppEvents, Props } from "../AppEvents";
78

@@ -32,51 +33,53 @@ jest.mock("@react-native-community/netinfo", () => ({
3233
})
3334
}));
3435

35-
const defaultProps: Props = {
36-
name: "app-events-test",
37-
onResumeTimeout: 0,
38-
onOnlineTimeout: 0,
39-
onOfflineTimeout: 0,
40-
delayTime: 30,
41-
timerType: "once",
42-
style: []
43-
};
44-
4536
describe("AppEvents", () => {
37+
let defaultProps: Props;
38+
39+
beforeEach(() => {
40+
defaultProps = {
41+
name: "app-events-test",
42+
onResumeTimeout: 0,
43+
onOnlineTimeout: 0,
44+
onOfflineTimeout: 0,
45+
delayTime: 30,
46+
timerType: "once",
47+
style: []
48+
};
49+
});
50+
4651
afterEach(() => {
4752
appStateChangeHandler = undefined;
4853
connectionChangeHandler = undefined;
4954
// setTimeout(); NodeJS.Timeout;
5055
});
5156

5257
it("does not render anything", () => {
53-
const component = render(<AppEvents {...defaultProps} />);
54-
55-
expect(component.toJSON()).toBeNull();
58+
const wrapper = mount(<AppEvents {...defaultProps} />);
59+
expect(wrapper).toMatchObject({});
5660
});
5761

5862
describe("with on load action", () => {
5963
it("executes the on load action", () => {
6064
const onLoadAction = actionValue();
61-
const { update } = render(<AppEvents {...defaultProps} onLoadAction={onLoadAction} />);
62-
update(<AppEvents {...defaultProps} onLoadAction={onLoadAction} />);
65+
mount(<AppEvents {...defaultProps} onLoadAction={onLoadAction} />);
6366
expect(onLoadAction.execute).toHaveBeenCalledTimes(1);
6467
});
6568
});
6669

6770
describe("with on resume action", () => {
6871
it("registers and unregisters an event listener", () => {
6972
const onResumeAction = actionValue();
70-
const component = render(<AppEvents {...defaultProps} onResumeAction={onResumeAction} />);
73+
const wrapper = mount(<AppEvents {...defaultProps} onResumeAction={onResumeAction} />);
7174

7275
expect(appStateChangeHandler).toBeDefined();
73-
component.unmount();
76+
wrapper.unmount();
7477
expect(appStateChangeHandler).toBeUndefined();
7578
});
7679

7780
it("executes the on resume action", () => {
7881
const onResumeAction = actionValue();
79-
render(<AppEvents {...defaultProps} onResumeAction={onResumeAction} />);
82+
mount(<AppEvents {...defaultProps} onResumeAction={onResumeAction} />);
8083

8184
appStateChangeHandler!("background");
8285
appStateChangeHandler!("active");
@@ -85,7 +88,7 @@ describe("AppEvents", () => {
8588

8689
it("does not execute the on resume action when the app state hasn't changed", () => {
8790
const onResumeAction = actionValue();
88-
render(<AppEvents {...defaultProps} onResumeAction={onResumeAction} />);
91+
mount(<AppEvents {...defaultProps} onResumeAction={onResumeAction} />);
8992

9093
appStateChangeHandler!("active");
9194
appStateChangeHandler!("active");
@@ -96,7 +99,7 @@ describe("AppEvents", () => {
9699
const dateNowSpy = jest.spyOn(Date, "now").mockReturnValue(0);
97100

98101
const onResumeAction = actionValue();
99-
render(<AppEvents {...defaultProps} onResumeAction={onResumeAction} onResumeTimeout={5} />);
102+
mount(<AppEvents {...defaultProps} onResumeAction={onResumeAction} onResumeTimeout={5} />);
100103

101104
dateNowSpy.mockReturnValue(4000);
102105
appStateChangeHandler!("background");
@@ -115,17 +118,17 @@ describe("AppEvents", () => {
115118
describe("with on online action", () => {
116119
it("registers and unregisters an event listener", async () => {
117120
const onOnlineAction = actionValue();
118-
const component = render(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} />);
121+
const wrapper = mount(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} />);
119122
await flushMicrotasksQueue();
120123

121124
expect(connectionChangeHandler).toBeDefined();
122-
component.unmount();
125+
wrapper.unmount();
123126
expect(connectionChangeHandler).toBeUndefined();
124127
});
125128

126129
it("executes the on online action", async () => {
127130
const onOnlineAction = actionValue();
128-
render(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} />);
131+
mount(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} />);
129132
await flushMicrotasksQueue();
130133

131134
connectionChangeHandler!({ isConnected: false });
@@ -137,7 +140,7 @@ describe("AppEvents", () => {
137140
const dateNowSpy = jest.spyOn(Date, "now").mockReturnValue(0);
138141

139142
const onOnlineAction = actionValue();
140-
render(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} onOnlineTimeout={5} />);
143+
mount(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} onOnlineTimeout={5} />);
141144
await flushMicrotasksQueue();
142145

143146
dateNowSpy.mockReturnValue(4000);
@@ -155,7 +158,7 @@ describe("AppEvents", () => {
155158

156159
it("does not execute the on online action if the connection state didn't change", async () => {
157160
const onOnlineAction = actionValue();
158-
render(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} />);
161+
mount(<AppEvents {...defaultProps} onOnlineAction={onOnlineAction} />);
159162
await flushMicrotasksQueue();
160163

161164
connectionChangeHandler!({ isConnected: true });
@@ -176,7 +179,7 @@ describe("AppEvents", () => {
176179

177180
it("executes the on timeout action once after the timeout has passed", () => {
178181
const onTimeoutAction = actionValue();
179-
render(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} />);
182+
mount(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} />);
180183

181184
expect(onTimeoutAction.execute).toHaveBeenCalledTimes(0);
182185
jest.advanceTimersByTime(30000);
@@ -187,16 +190,16 @@ describe("AppEvents", () => {
187190

188191
it("does not execute the on timeout action after the component has been unmounted", () => {
189192
const onTimeoutAction = actionValue();
190-
const component = render(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} />);
193+
const wrapper = mount(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} />);
191194
jest.advanceTimersByTime(15000);
192-
component.unmount();
195+
wrapper.unmount();
193196
jest.advanceTimersByTime(15000);
194197
expect(onTimeoutAction.execute).toHaveBeenCalledTimes(0);
195198
});
196199

197200
it("executes the interval on timeout action after every interval", () => {
198201
const onTimeoutAction = actionValue();
199-
render(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} timerType={"interval"} />);
202+
mount(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} timerType={"interval"} />);
200203

201204
expect(onTimeoutAction.execute).toHaveBeenCalledTimes(0);
202205
jest.advanceTimersByTime(30000);
@@ -207,20 +210,20 @@ describe("AppEvents", () => {
207210

208211
it("does not execute the interval on timeout action after the component has been unmounted", () => {
209212
const onTimeoutAction = actionValue();
210-
const component = render(
213+
const wrapper = mount(
211214
<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} timerType={"interval"} />
212215
);
213216

214217
jest.advanceTimersByTime(30000);
215218
expect(onTimeoutAction.execute).toHaveBeenCalledTimes(1);
216-
component.unmount();
219+
wrapper.unmount();
217220
jest.advanceTimersByTime(30000);
218221
expect(onTimeoutAction.execute).toHaveBeenCalledTimes(1);
219222
});
220223

221224
it("does not execute the interval on timeout action when it is already executing", () => {
222225
const onTimeoutAction = actionValue(true, true);
223-
render(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} timerType={"interval"} />);
226+
mount(<AppEvents {...defaultProps} onTimeoutAction={onTimeoutAction} timerType={"interval"} />);
224227

225228
jest.advanceTimersByTime(30000);
226229
expect(onTimeoutAction.execute).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)