forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createControllerComponent.spec.tsx
120 lines (103 loc) 路 3.4 KB
/
createControllerComponent.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React from 'react';
import { Components } from '@ionic/core';
import { createControllerComponent } from '../createControllerComponent';
import { render, waitForElement, wait } from 'react-testing-library';
import * as utils from '../utils';
import { createControllerUtils } from '../utils/controller-test-utils';
import 'jest-dom/extend-expect';
describe('createControllerComponent - events', () => {
const { cleanupAfterController, createControllerElement, augmentController} = createControllerUtils('ion-loading');
type LoadingOptions = Components.IonLoadingAttributes;
const IonLoading = createControllerComponent<LoadingOptions, HTMLIonLoadingElement, HTMLIonLoadingControllerElement>('ion-loading', 'ion-loading-controller')
afterEach(cleanupAfterController);
test('should create controller component outside of the react component', async () => {
const { container, baseElement } = render(
<>
<IonLoading
isOpen={false}
onDidDismiss={jest.fn()}
duration={2000}
>
</IonLoading>
<span>ButtonNameA</span>
</>
);
expect(container).toContainHTML('<div><span>ButtonNameA</span></div>');
expect(baseElement.querySelector('ion-loading-controller')).toBeInTheDocument();
});
test('should create component and attach props on opening', async () => {
const onDidDismiss = jest.fn();
const { baseElement, container, rerender } = render(
<IonLoading
isOpen={false}
onDidDismiss={onDidDismiss}
duration={2000}
>
ButtonNameA
</IonLoading>
);
const [element, presentFunction] = createControllerElement();
const loadingController = augmentController(baseElement, container, element);
const attachEventPropsSpy = jest.spyOn(utils, "attachEventProps");
rerender(
<IonLoading
isOpen={true}
onDidDismiss={onDidDismiss}
duration={2000}
>
ButtonNameA
</IonLoading>
);
await waitForElement(() => container.querySelector('ion-loading'));
expect((loadingController as any).create).toHaveBeenCalledWith({
duration: 2000,
children: 'ButtonNameA',
onIonLoadingDidDismiss: onDidDismiss
});
expect(presentFunction).toHaveBeenCalled();
expect(attachEventPropsSpy).toHaveBeenCalledWith(element, {
duration: 2000,
children: 'ButtonNameA',
onIonLoadingDidDismiss: onDidDismiss
});
});
test('should dismiss component on hiding', async () => {
const { container, baseElement, rerender } = render(
<IonLoading
isOpen={false}
onDidDismiss={jest.fn()}
duration={2000}
>
ButtonNameA
</IonLoading>
);
const [element, , dismissFunction] = createControllerElement();
augmentController(baseElement, container, element);
rerender(
<IonLoading
isOpen={true}
onDidDismiss={jest.fn()}
duration={2000}
>
ButtonNameA
</IonLoading>
);
await waitForElement(() => container.querySelector('ion-loading'));
rerender(
<IonLoading
isOpen={false}
onDidDismiss={jest.fn()}
duration={2000}
>
ButtonNameA
</IonLoading>
);
await wait(() => {
const item = container.querySelector('ion-loading');
if (item) {
throw new Error();
}
});
expect(dismissFunction).toHaveBeenCalled();
});
});