-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathSaveNewPolicyModal.tests.tsx
More file actions
229 lines (200 loc) · 7 KB
/
SaveNewPolicyModal.tests.tsx
File metadata and controls
229 lines (200 loc) · 7 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React from "react";
import { screen, waitFor } from "@testing-library/react";
import { createCustomRenderer } from "test/test-utils";
import createMockUser from "__mocks__/userMock";
import createMockConfig from "__mocks__/configMock";
import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw";
import mockServer from "test/mock-server";
import { ILabelSummary } from "interfaces/label";
import PolicyProvider from "context/policy";
import SaveNewPolicyModal from "./SaveNewPolicyModal";
const baseUrl = (path: string) => {
return `/api/latest/fleet${path}`;
};
const mockLabels: ILabelSummary[] = [
{
id: 1,
name: "Fun",
description: "Computers that like to have a good time",
label_type: "regular",
},
{
id: 2,
name: "Fresh",
description: "Laptops with dirty mouths",
label_type: "regular",
},
];
const labelSummariesHandler = http.get(baseUrl("/labels/summary"), () => {
return HttpResponse.json({
labels: mockLabels,
});
});
describe("SaveNewPolicyModal", () => {
const defaultProps = {
baseClass: "",
queryValue: "",
onCreatePolicy: jest.fn(),
setIsSaveNewPolicyModalOpen: jest.fn(),
backendValidators: {},
platformSelector: {
setSelectedPlatforms: jest.fn(),
getSelectedPlatforms: () => {
return [];
},
isAnyPlatformSelected: true,
render: () => <div />,
disabled: false,
},
isUpdatingPolicy: false,
isFetchingAutofillDescription: false,
isFetchingAutofillResolution: false,
onClickAutofillDescription: jest.fn(),
onClickAutofillResolution: jest.fn(),
labels: mockLabels,
};
it("should not show the target selector in the free tier", async () => {
const render = createCustomRenderer({
withBackendMock: true,
context: {
app: {
currentUser: createMockUser(),
config: createMockConfig(),
isPremiumTier: false,
},
},
});
render(<SaveNewPolicyModal {...defaultProps} />);
// Wait for any queries (that should not be happening) to finish.
await new Promise((resolve) => setTimeout(resolve, 500));
// Check that the target selector is not present.
expect(screen.queryByText("All hosts")).not.toBeInTheDocument();
});
describe("in premium tier", () => {
const render = createCustomRenderer({
withBackendMock: true,
context: {
app: {
currentUser: createMockUser(),
isGlobalObserver: false,
isGlobalAdmin: true,
isGlobalMaintainer: false,
isOnGlobalTeam: true,
isPremiumTier: true,
isSandboxMode: false,
config: createMockConfig(),
},
},
});
beforeEach(() => {
mockServer.use(labelSummariesHandler);
});
it("should show the target selector in All hosts target mode when the policy has no labels", async () => {
render(<SaveNewPolicyModal {...defaultProps} />);
await waitFor(() => {
expect(screen.getByLabelText("All hosts")).toBeInTheDocument();
expect(screen.getByLabelText("Custom")).toBeInTheDocument();
expect(screen.getByLabelText("All hosts")).toBeChecked();
});
});
it("should disable the save button in Custom target mode when no labels are selected, and enable it once labels are selected", async () => {
render(<SaveNewPolicyModal {...defaultProps} />);
let allHosts;
let custom;
await waitFor(() => {
allHosts = screen.getByLabelText("All hosts");
custom = screen.getByLabelText("Custom");
expect(allHosts).toBeInTheDocument();
expect(custom).toBeInTheDocument();
});
custom && (await userEvent.click(custom));
const saveButton = screen.getByRole("button", { name: "Save" });
expect(saveButton).toBeDisabled();
const funButton = await screen.findByRole("checkbox", {
name: "Fun",
});
expect(funButton).not.toBeChecked();
await userEvent.click(funButton);
expect(saveButton).toBeEnabled();
});
it("should send labels when saving a new policy in Custom target mode (include any)", async () => {
const onCreatePolicy = jest.fn();
const props = { ...defaultProps, onCreatePolicy };
render(
<PolicyProvider>
<SaveNewPolicyModal {...props} />
</PolicyProvider>
);
await waitFor(() => {
expect(screen.getByLabelText("All hosts")).toBeInTheDocument();
});
// Set a name.
const nameInput = screen.getByLabelText("Name");
await userEvent.type(nameInput, "A Brand New Policy!");
// Set a label.
await userEvent.click(screen.getByLabelText("Custom"));
await userEvent.click(
await screen.findByRole("checkbox", { name: "Fun" })
);
await userEvent.click(screen.getByRole("button", { name: "Save" }));
expect(onCreatePolicy.mock.calls[0][0].labels_include_any).toEqual([
"Fun",
]);
});
it("should send labels when saving a new policy in Custom target mode (exclude any)", async () => {
const onCreatePolicy = jest.fn();
const props = { ...defaultProps, onCreatePolicy };
render(
<PolicyProvider>
<SaveNewPolicyModal {...props} />
</PolicyProvider>
);
await waitFor(() => {
expect(screen.getByLabelText("All hosts")).toBeInTheDocument();
});
// Set a name.
const nameInput = screen.getByLabelText("Name");
await userEvent.type(nameInput, "A Brand New Policy!");
// Set a label.
await userEvent.click(screen.getByLabelText("Custom"));
await userEvent.click(
await screen.findByRole("checkbox", { name: "Fun" })
);
// Click "Include any" to open the dropdown.
const includeAnyOption = screen.getByRole("option", {
name: "Include any",
});
await userEvent.click(includeAnyOption);
// Click "Exclude any" to select it.
let excludeAnyOption: unknown;
await waitFor(() => {
excludeAnyOption = screen.getByRole("option", { name: "Exclude any" });
});
await userEvent.click(excludeAnyOption as Element);
await userEvent.click(screen.getByRole("button", { name: "Save" }));
expect(onCreatePolicy.mock.calls[0][0].labels_exclude_any).toEqual([
"Fun",
]);
});
it("should clear labels when saving a new policy in All hosts target mode", async () => {
const onCreatePolicy = jest.fn();
const props = { ...defaultProps, onCreatePolicy };
render(
<PolicyProvider>
<SaveNewPolicyModal {...props} />
</PolicyProvider>
);
await waitFor(() => {
expect(screen.getByLabelText("All hosts")).toBeInTheDocument();
});
// Set a name.
await userEvent.type(
screen.getByLabelText("Name"),
"A Brand New Policy!"
);
await userEvent.click(screen.getByRole("button", { name: "Save" }));
expect(onCreatePolicy.mock.calls[0][0].labels_include_any).toEqual([]);
});
});
});