-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1455 from flanksource/1445-playbook-improvements
1445 playbook improvements
- Loading branch information
Showing
13 changed files
with
332 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ts/Playbooks/Runs/SelectPlaybookToRun.tsx → ...books/Runs/Submit/SelectPlaybookToRun.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
.../Playbooks/Runs/SubmitPlaybookRunForm.tsx → ...oks/Runs/Submit/SubmitPlaybookRunForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/components/Playbooks/Runs/Submit/__tests__/SelectPlaybookToRun.unit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { rest } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { PlaybookSpec } from "../../../Settings/PlaybookSpecsTable"; | ||
import SelectPlaybookToRun from "./../SelectPlaybookToRun"; | ||
|
||
const playbooks: PlaybookSpec[] = [ | ||
{ | ||
id: "1", | ||
name: "Playbook 1", | ||
created_at: "2021-09-01T00:00:00Z", | ||
source: "UI", | ||
spec: {}, | ||
updated_at: "2021-09-01T00:00:00Z" | ||
}, | ||
{ | ||
id: "2", | ||
name: "Playbook 2", | ||
created_at: "2021-09-01T00:00:00Z", | ||
source: "UI", | ||
spec: {}, | ||
updated_at: "2021-09-01T00:00:00Z" | ||
} | ||
]; | ||
|
||
global.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
observe: jest.fn(), | ||
unobserve: jest.fn(), | ||
disconnect: jest.fn() | ||
})); | ||
|
||
// Define a mock server to handle PATCH requests | ||
const server = setupServer( | ||
rest.get("/api/playbook/list", (req, res, ctx) => { | ||
return res(ctx.json(playbooks)); | ||
}) | ||
); | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
describe("SelectPlaybookToRun", () => { | ||
it("should render dropdown list with playbooks", async () => { | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<SelectPlaybookToRun component_id="component_id" /> | ||
</QueryClientProvider> | ||
); | ||
|
||
const playbooksButton = await screen.findByRole("button", { | ||
name: /playbooks/i | ||
}); | ||
|
||
userEvent.click(playbooksButton); | ||
|
||
expect(await screen.findByText(/playbook 1/i)).toBeInTheDocument(); | ||
expect(await screen.findByText(/playbook 2/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it("should open runs page, when you click a playbook item", async () => { | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<SelectPlaybookToRun check_id="check_id" /> | ||
</QueryClientProvider> | ||
); | ||
|
||
const playbooksButton = await screen.findByRole("button", { | ||
name: /playbooks/i | ||
}); | ||
|
||
userEvent.click(playbooksButton); | ||
|
||
const playbook1 = await screen.findByText(/playbook 1/i); | ||
|
||
userEvent.click(playbook1); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByRole("heading", { level: 1, name: /playbook 1/i }) | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
111 changes: 111 additions & 0 deletions
111
src/components/Playbooks/Runs/Submit/__tests__/SubmitPlaybookRunForm.unit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { rest } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { PlaybookSpec } from "../../../Settings/PlaybookSpecsTable"; | ||
import SubmitPlaybookRunForm from "./../SubmitPlaybookRunForm"; | ||
|
||
const playbookSpec: PlaybookSpec = { | ||
id: "1", | ||
name: "Playbook 1", | ||
source: "UI", | ||
spec: { | ||
parameters: [ | ||
{ | ||
label: "Label", | ||
name: "name" | ||
} | ||
] | ||
}, | ||
created_at: "2021-09-01T00:00:00Z", | ||
updated_at: "2021-09-01T00:00:00Z" | ||
}; | ||
|
||
global.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
observe: jest.fn(), | ||
unobserve: jest.fn(), | ||
disconnect: jest.fn() | ||
})); | ||
|
||
// Define a mock server to handle PATCH requests | ||
const server = setupServer( | ||
rest.post("/api/playbook/run", (req, res, ctx) => { | ||
return res(ctx.json(playbookSpec)); | ||
}) | ||
); | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
describe("SubmitPlaybookRunForm", () => { | ||
const componentId = "component-1"; | ||
const checkId = "check-1"; | ||
const configId = "config-1"; | ||
|
||
it("should render the form with the correct initial values", async () => { | ||
const closeFn = jest.fn(); | ||
|
||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<SubmitPlaybookRunForm | ||
isOpen={true} | ||
onClose={closeFn} | ||
playbookSpec={playbookSpec} | ||
componentId={componentId} | ||
checkId={checkId} | ||
configId={configId} | ||
/> | ||
</QueryClientProvider> | ||
); | ||
|
||
expect( | ||
await screen.findByRole("heading", { level: 1, name: /Playbook 1/i }) | ||
).toBeInTheDocument(); | ||
|
||
expect(screen.getByLabelText("Label")).toBeInTheDocument(); | ||
|
||
expect(screen.getByRole("button", { name: /Submit/i })).toBeInTheDocument(); | ||
|
||
userEvent.click(screen.getByRole("button", { name: /close/i })); | ||
|
||
await waitFor(() => { | ||
expect(closeFn).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it("should submit the form when the submit button is clicked", async () => { | ||
const closeFn = jest.fn(); | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<SubmitPlaybookRunForm | ||
isOpen={true} | ||
onClose={closeFn} | ||
playbookSpec={playbookSpec} | ||
componentId={componentId} | ||
checkId={checkId} | ||
configId={configId} | ||
/> | ||
</QueryClientProvider> | ||
); | ||
|
||
expect( | ||
await screen.findByRole("heading", { level: 1, name: /Playbook 1/i }) | ||
).toBeInTheDocument(); | ||
|
||
const input = screen.getByLabelText("Label"); | ||
|
||
fireEvent.change(input, { target: { value: "test" } }); | ||
|
||
const btn = screen.getByRole("button", { name: /Submit/i }); | ||
|
||
userEvent.click(btn); | ||
|
||
await waitFor(() => { | ||
expect(closeFn).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.