|
| 1 | +import { renderComponent } from "testHelpers/renderHelpers"; |
| 2 | +import { ProvisionerTagsPopover } from "./ProvisionerTagsPopover"; |
| 3 | +import { fireEvent, screen } from "@testing-library/react"; |
| 4 | +import { MockTemplateVersion } from "testHelpers/entities"; |
| 5 | +import userEvent from "@testing-library/user-event"; |
| 6 | + |
| 7 | +let tags = MockTemplateVersion.job.tags; |
| 8 | + |
| 9 | +describe("ProvisionerTagsPopover", () => { |
| 10 | + describe("click the button", () => { |
| 11 | + it("can add a tag", async () => { |
| 12 | + const onSubmit = jest.fn().mockImplementation(({key, value}) => { |
| 13 | + tags = {...tags, [key]: value} |
| 14 | + }); |
| 15 | + const onDelete = jest.fn().mockImplementation((key) => { |
| 16 | + const newTags = {...tags} |
| 17 | + delete newTags[key] |
| 18 | + tags = newTags |
| 19 | + }); |
| 20 | + const {rerender} = renderComponent( |
| 21 | + <ProvisionerTagsPopover |
| 22 | + tags={tags} |
| 23 | + onSubmit={onSubmit} |
| 24 | + onDelete={onDelete} |
| 25 | + /> |
| 26 | + ); |
| 27 | + |
| 28 | + // Open Popover |
| 29 | + const btn = await screen.findByRole("button"); |
| 30 | + expect(btn).toBeEnabled(); |
| 31 | + await userEvent.click(btn); |
| 32 | + |
| 33 | + // Check for existing tags |
| 34 | + const el = await screen.findByText(/scope: organization/i); |
| 35 | + expect(el).toBeInTheDocument(); |
| 36 | + |
| 37 | + // Add key and value |
| 38 | + const el2 = await screen.findByLabelText("Key"); |
| 39 | + expect(el2).toBeEnabled(); |
| 40 | + fireEvent.change(el2, { target: { value: "foo" } }); |
| 41 | + expect(el2).toHaveValue("foo"); |
| 42 | + const el3 = await screen.findByLabelText("Value"); |
| 43 | + expect(el3).toBeEnabled(); |
| 44 | + fireEvent.change(el3, { target: { value: "bar" } }); |
| 45 | + expect(el3).toHaveValue("bar"); |
| 46 | + |
| 47 | + // Submit |
| 48 | + const btn2 = await screen.findByRole("button", { name: /add/i, hidden: true}); |
| 49 | + expect(btn2).toBeEnabled(); |
| 50 | + await userEvent.click(btn2); |
| 51 | + expect(onSubmit).toHaveBeenCalledTimes(1); |
| 52 | + |
| 53 | + rerender( |
| 54 | + <ProvisionerTagsPopover |
| 55 | + tags={tags} |
| 56 | + onSubmit={onSubmit} |
| 57 | + onDelete={onDelete} |
| 58 | + /> |
| 59 | + ); |
| 60 | + |
| 61 | + // Check for new tag |
| 62 | + const el4 = await screen.findByText(/foo: bar/i); |
| 63 | + expect(el4).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + it("can remove a tag", async () => { |
| 66 | + const onSubmit = jest.fn().mockImplementation(({key, value}) => { |
| 67 | + tags = {...tags, [key]: value} |
| 68 | + }); |
| 69 | + const onDelete = jest.fn().mockImplementation((key) => { |
| 70 | + delete tags[key] |
| 71 | + tags = {...tags} |
| 72 | + }); |
| 73 | + const {rerender} = renderComponent( |
| 74 | + <ProvisionerTagsPopover |
| 75 | + tags={tags} |
| 76 | + onSubmit={onSubmit} |
| 77 | + onDelete={onDelete} |
| 78 | + /> |
| 79 | + ); |
| 80 | + |
| 81 | + // Open Popover |
| 82 | + const btn = await screen.findByRole("button"); |
| 83 | + expect(btn).toBeEnabled(); |
| 84 | + await userEvent.click(btn); |
| 85 | + |
| 86 | + // Check for existing tags |
| 87 | + const el = await screen.findByText(/wowzers: whatatag/i); |
| 88 | + expect(el).toBeInTheDocument(); |
| 89 | + |
| 90 | + // Find Delete button |
| 91 | + const btn2 = await screen.findByRole("button", { name: /delete-wowzers/i, hidden: true}); |
| 92 | + expect(btn2).toBeEnabled(); |
| 93 | + |
| 94 | + // Delete tag |
| 95 | + await userEvent.click(btn2); |
| 96 | + expect(onDelete).toHaveBeenCalledTimes(1); |
| 97 | + |
| 98 | + rerender( |
| 99 | + <ProvisionerTagsPopover |
| 100 | + tags={tags} |
| 101 | + onSubmit={onSubmit} |
| 102 | + onDelete={onDelete} |
| 103 | + /> |
| 104 | + ); |
| 105 | + |
| 106 | + // Expect deleted tag to be gone |
| 107 | + const el2 = screen.queryByText(/wowzers: whatatag/i); |
| 108 | + expect(el2).not.toBeInTheDocument(); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments