-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Expand file tree
/
Copy pathutils.quickstart.test.tsx
More file actions
127 lines (106 loc) · 4.73 KB
/
utils.quickstart.test.tsx
File metadata and controls
127 lines (106 loc) · 4.73 KB
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
121
122
123
124
125
126
127
import * as React from 'react';
import * as renderer from 'react-test-renderer';
import {BehaviorSubject, Subscription} from 'rxjs';
import {ContextApis} from '../../shared/context';
import {AbstractApplication, Application, ApplicationTree, ResourceNode} from '../../shared/models';
import {services} from '../../shared/services';
import {renderResourceButtons} from './utils';
describe('renderResourceButtons', () => {
const resource = {
uid: 'apps/Deployment/default/guestbook',
group: 'apps',
kind: 'Deployment',
namespace: 'default',
name: 'guestbook'
} as ResourceNode;
const otherResource = {
...resource,
uid: 'apps/Deployment/default/guestbook-canary',
name: 'guestbook-canary'
} as ResourceNode;
const application = {
kind: 'Application',
metadata: {name: 'guestbook', namespace: 'argocd'},
spec: {project: 'default'},
status: {
resources: [
{
group: resource.group,
kind: resource.kind,
namespace: resource.namespace,
name: resource.name
}
]
}
} as Application;
const tree = {
nodes: [resource]
} as ApplicationTree;
const apis = {
popup: {prompt: jest.fn()},
notifications: {show: jest.fn()},
navigation: {goto: jest.fn()},
baseHref: ''
} as unknown as ContextApis;
afterEach(() => {
jest.restoreAllMocks();
});
function QuickStartWrapper(props: {resourceNode: ResourceNode; version: number; appChanged: BehaviorSubject<AbstractApplication>}) {
return <div data-version={props.version}>{renderResourceButtons(props.resourceNode, application, tree, apis, props.appChanged)}</div>;
}
it('does not reload quickstart actions on unrelated parent rerenders', () => {
const canISpy = jest.spyOn(services.accounts, 'canI').mockResolvedValue(false);
const appChanged = new BehaviorSubject<AbstractApplication>(application);
const subscription: Subscription = appChanged.subscribe(() => undefined);
let rendered: renderer.ReactTestRenderer | undefined;
try {
renderer.act(() => {
rendered = renderer.create(<QuickStartWrapper resourceNode={resource} version={0} appChanged={appChanged} />);
});
expect(canISpy).toHaveBeenCalledTimes(1);
renderer.act(() => {
rendered!.update(<QuickStartWrapper resourceNode={resource} version={1} appChanged={appChanged} />);
});
expect(canISpy).toHaveBeenCalledTimes(1);
} finally {
subscription.unsubscribe();
}
});
it('reloads quickstart actions when the resource changes', () => {
const canISpy = jest.spyOn(services.accounts, 'canI').mockResolvedValue(false);
const appChanged = new BehaviorSubject<AbstractApplication>(application);
const subscription: Subscription = appChanged.subscribe(() => undefined);
let rendered: renderer.ReactTestRenderer | undefined;
try {
renderer.act(() => {
rendered = renderer.create(<QuickStartWrapper resourceNode={resource} version={0} appChanged={appChanged} />);
});
renderer.act(() => {
rendered!.update(<QuickStartWrapper resourceNode={otherResource} version={0} appChanged={appChanged} />);
});
expect(canISpy).toHaveBeenCalledTimes(2);
} finally {
subscription.unsubscribe();
}
});
it('reloads quickstart actions when the appChanged subject identity changes', () => {
const canISpy = jest.spyOn(services.accounts, 'canI').mockResolvedValue(false);
const firstAppChanged = new BehaviorSubject<AbstractApplication>(application);
const secondAppChanged = new BehaviorSubject<AbstractApplication>(application);
const firstSubscription: Subscription = firstAppChanged.subscribe(() => undefined);
const secondSubscription: Subscription = secondAppChanged.subscribe(() => undefined);
let rendered: renderer.ReactTestRenderer | undefined;
try {
renderer.act(() => {
rendered = renderer.create(<QuickStartWrapper resourceNode={resource} version={0} appChanged={firstAppChanged} />);
});
renderer.act(() => {
rendered!.update(<QuickStartWrapper resourceNode={resource} version={0} appChanged={secondAppChanged} />);
});
expect(canISpy).toHaveBeenCalledTimes(2);
} finally {
firstSubscription.unsubscribe();
secondSubscription.unsubscribe();
}
});
});