|
1 |
| -import { renderWithAuth } from "testHelpers/renderHelpers"; |
| 1 | +import { |
| 2 | + renderWithAuth, |
| 3 | + waitForLoaderToBeRemoved, |
| 4 | +} from "testHelpers/renderHelpers"; |
2 | 5 | import TemplateVersionEditorPage from "./TemplateVersionEditorPage";
|
3 |
| -import { screen, waitFor, within } from "@testing-library/react"; |
| 6 | +import { render, screen, waitFor, within } from "@testing-library/react"; |
4 | 7 | import userEvent from "@testing-library/user-event";
|
5 | 8 | import * as api from "api/api";
|
6 | 9 | import {
|
7 | 10 | MockTemplate,
|
8 | 11 | MockTemplateVersion,
|
| 12 | + MockTemplateVersionVariable1, |
| 13 | + MockTemplateVersionVariable2, |
9 | 14 | MockWorkspaceBuildLogs,
|
10 | 15 | } from "testHelpers/entities";
|
11 | 16 | import { Language } from "./PublishTemplateVersionDialog";
|
| 17 | +import { QueryClient } from "react-query"; |
| 18 | +import { templateVersionVariablesKey } from "api/queries/templates"; |
| 19 | +import { RouterProvider, createMemoryRouter } from "react-router-dom"; |
| 20 | +import { RequireAuth } from "contexts/auth/RequireAuth"; |
| 21 | +import { server } from "testHelpers/server"; |
| 22 | +import { rest } from "msw"; |
| 23 | +import { AppProviders } from "App"; |
12 | 24 |
|
13 | 25 | // For some reason this component in Jest is throwing a MUI style warning so,
|
14 | 26 | // since we don't need it for this test, we can mock it out
|
@@ -208,3 +220,111 @@ test("Patch request is not send when there are no changes", async () => {
|
208 | 220 | );
|
209 | 221 | expect(patchTemplateVersion).toBeCalledTimes(0);
|
210 | 222 | });
|
| 223 | + |
| 224 | +describe.each([ |
| 225 | + { |
| 226 | + testName: "Do not ask when template version has no errors", |
| 227 | + initialVariables: undefined, |
| 228 | + loadedVariables: undefined, |
| 229 | + templateVersion: MockTemplateVersion, |
| 230 | + askForVariables: false, |
| 231 | + }, |
| 232 | + { |
| 233 | + testName: |
| 234 | + "Do not ask when template version has no errors even when having previously loaded variables", |
| 235 | + initialVariables: [ |
| 236 | + MockTemplateVersionVariable1, |
| 237 | + MockTemplateVersionVariable2, |
| 238 | + ], |
| 239 | + loadedVariables: undefined, |
| 240 | + templateVersion: MockTemplateVersion, |
| 241 | + askForVariables: false, |
| 242 | + }, |
| 243 | + { |
| 244 | + testName: "Ask when template version has errors", |
| 245 | + initialVariables: undefined, |
| 246 | + templateVersion: { |
| 247 | + ...MockTemplateVersion, |
| 248 | + job: { |
| 249 | + ...MockTemplateVersion.job, |
| 250 | + error_code: "REQUIRED_TEMPLATE_VARIABLES", |
| 251 | + }, |
| 252 | + }, |
| 253 | + loadedVariables: [ |
| 254 | + MockTemplateVersionVariable1, |
| 255 | + MockTemplateVersionVariable2, |
| 256 | + ], |
| 257 | + askForVariables: true, |
| 258 | + }, |
| 259 | +])( |
| 260 | + "Missing template variables", |
| 261 | + ({ |
| 262 | + testName, |
| 263 | + initialVariables, |
| 264 | + loadedVariables, |
| 265 | + templateVersion, |
| 266 | + askForVariables, |
| 267 | + }) => { |
| 268 | + it(testName, async () => { |
| 269 | + jest.resetAllMocks(); |
| 270 | + const queryClient = new QueryClient(); |
| 271 | + queryClient.setQueryData( |
| 272 | + templateVersionVariablesKey(MockTemplateVersion.id), |
| 273 | + initialVariables, |
| 274 | + ); |
| 275 | + |
| 276 | + server.use( |
| 277 | + rest.get( |
| 278 | + "/api/v2/organizations/:org/templates/:template/versions/:version", |
| 279 | + (req, res, ctx) => { |
| 280 | + return res(ctx.json(templateVersion)); |
| 281 | + }, |
| 282 | + ), |
| 283 | + ); |
| 284 | + |
| 285 | + if (loadedVariables) { |
| 286 | + server.use( |
| 287 | + rest.get( |
| 288 | + "/api/v2/templateversions/:version/variables", |
| 289 | + (req, res, ctx) => { |
| 290 | + return res(ctx.json(loadedVariables)); |
| 291 | + }, |
| 292 | + ), |
| 293 | + ); |
| 294 | + } |
| 295 | + |
| 296 | + render( |
| 297 | + <AppProviders queryClient={queryClient}> |
| 298 | + <RouterProvider |
| 299 | + router={createMemoryRouter( |
| 300 | + [ |
| 301 | + { |
| 302 | + element: <RequireAuth />, |
| 303 | + children: [ |
| 304 | + { |
| 305 | + element: <TemplateVersionEditorPage />, |
| 306 | + path: "/templates/:template/versions/:version/edit", |
| 307 | + }, |
| 308 | + ], |
| 309 | + }, |
| 310 | + ], |
| 311 | + { |
| 312 | + initialEntries: [ |
| 313 | + `/templates/${MockTemplate.name}/versions/${MockTemplateVersion.name}/edit`, |
| 314 | + ], |
| 315 | + }, |
| 316 | + )} |
| 317 | + /> |
| 318 | + </AppProviders>, |
| 319 | + ); |
| 320 | + await waitForLoaderToBeRemoved(); |
| 321 | + |
| 322 | + const dialogSelector = /template variables/i; |
| 323 | + if (askForVariables) { |
| 324 | + await screen.findByText(dialogSelector); |
| 325 | + } else { |
| 326 | + expect(screen.queryByText(dialogSelector)).not.toBeInTheDocument(); |
| 327 | + } |
| 328 | + }); |
| 329 | + }, |
| 330 | +); |
0 commit comments