Skip to content

Commit

Permalink
🧪 test(persistent-query): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Feb 15, 2024
1 parent cfeb040 commit e6051a4
Show file tree
Hide file tree
Showing 16 changed files with 434 additions and 63 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">
<a href="https://github.com/dashpresshq/dashpress">
<img src="https://dashpress.io/assets/logos/full-logo-white.png" alt="Logo" height="100">
<img src="https://dashpress.io/assets/banner/1.png" alt="Logo" >
</a>
</h1>

Expand Down Expand Up @@ -182,12 +182,6 @@ First off, thanks for taking the time to contribute! Contributions are what make

Please read [our contribution guidelines](docs/CONTRIBUTING.md), and thank you for being involved!

## Authors & contributors

The original setup of this repository is by [Ayobami Akingbade](https://github.com/thrownullexception).

For a full list of all authors and contributors, see [the contributors page](https://github.com/dashpresshq/dashpress/contributors).

## Security

DashPress takes security at heart and follows all known good practices of security, but 100% security cannot be assured.
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/_/api-handlers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const CONFIG_VALUES = {
createdAt: `created_at`,
updatedAt: `updated_at`,
},
users_to_database_link: {
table: "",
field: "",
},
disabled_entities: ["disabled-entity-1", "disabled-entity-2"],
menu_entities_order: [],
disabled_menu_entities: ["entity-3"],
Expand Down Expand Up @@ -122,6 +126,10 @@ const DEFAULT_ENTITY_CONFIG_VALUES: Record<
entity_relations_order: () => [],

hidden_entity_relations: () => ["hidden-related-entity-5"],
persistent_query: () => ({
children: [],
operator: "and",
}),
entity_columns_labels: () => ({}),
entity_presentation_script: () => "",
entity_relation_template: (entity) => ({
Expand Down
10 changes: 9 additions & 1 deletion src/__tests__/_/forCodeCoverage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import { FOR_CODE_COV as $42 } from "backend/menu/types";
import { FOR_CODE_COV as $43 } from "frontend/lib/form/types";
import { FOR_CODE_COV as $44 } from "frontend/design-system/components/Table/filters/types";
import { FOR_CODE_COV as $45 } from "shared/form-schemas/users";
import { FOR_CODE_COV as $46 } from "shared/constants/portal/menu/main";
import { FOR_CODE_COV as $47 } from "shared/constants/portal/menu/index";
import { FOR_CODE_COV as $48 } from "shared/user-preferences/base-types";
import { FOR_CODE_COV as $49 } from "shared/user-preferences/types";

import { noop } from "shared/lib/noop";

Expand Down Expand Up @@ -77,7 +81,11 @@ noop(
$42,
$43,
$44,
$45
$45,
$46,
$47,
$48,
$49
);

describe("Code coverage ignores plain types file", () => {
Expand Down
302 changes: 302 additions & 0 deletions src/__tests__/admin/[entity]/config/persistent-query.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
/* eslint-disable testing-library/no-node-access */
/* eslint-disable testing-library/no-container */
import React from "react";
import { render, screen } from "@testing-library/react";
import { ApplicationRoot } from "frontend/components/ApplicationRoot";
import userEvent from "@testing-library/user-event";
import { setupApiHandlers } from "__tests__/_/setupApihandlers";

import EntityPersistentQuerySettings from "pages/admin/[entity]/config/persistent-query";

setupApiHandlers();

describe("pages/admin/[entity]/config/persistent-query", () => {
const useRouter = jest.spyOn(require("next/router"), "useRouter");

useRouter.mockImplementation(() => ({
asPath: "/",
query: {
entity: "entity-1",
},
isReady: true,
}));

it("should save persistent queries", async () => {
render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

await userEvent.click(
await screen.findByRole("button", {
name: "Add Filter",
})
);

await userEvent.type(
await screen.findByLabelText("Field"),
"entity-1-string-field"
);
await userEvent.keyboard("{Enter}");

await userEvent.type(await screen.findByLabelText("Operator"), "Equal");
await userEvent.keyboard("{Enter}");

await userEvent.type(await screen.findByLabelText("Value"), "equal-value");

await userEvent.click(
screen.getByRole("button", {
name: "Save Persistent Query",
})
);

expect(await screen.findByRole("status")).toHaveTextContent(
"Persistent Query Saved Successfully"
);
});

it("should display updated value", async () => {
const { container } = render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

expect(
container.querySelector(`input[name="children[0].children[0].id"]`)
).toHaveValue("entity-1-string-field");

expect(
container.querySelector(
`input[name="children[0].children[0].value.operator"]`
)
).toHaveValue("e");

expect(
container.querySelector(
`input[name="children[0].children[0].value.value"]`
)
).toHaveValue("equal-value");
});

it("should add nested filters", async () => {
const { container } = render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

await userEvent.click(
await screen.findByRole("button", {
name: "Add Nested Filter",
})
);

await userEvent.type(
screen.getAllByLabelText("Field")[1],
"entity-1-date-field"
);
await userEvent.keyboard("{Enter}");

await userEvent.type(screen.getAllByLabelText("Operator")[1], "Less Than");
await userEvent.keyboard("{Enter}");

await userEvent.type(
screen.getAllByLabelText("Value")[1],
"less-than-value"
);

await userEvent.click(
screen.getByRole("button", {
name: "Add Nested Filter",
})
);

await userEvent.click(
screen.getAllByRole("button", {
name: "Remove Nested Filter",
})[2]
);

expect(
container.querySelector(`input[name="children[0].operator__and"]`)
).toBeChecked();

expect(
container.querySelector(`input[name="children[0].operator__or"`)
).not.toBeChecked();

await userEvent.click(
screen.getAllByRole("option", {
name: "OR",
})[0]
);

await userEvent.click(
screen.getByRole("button", {
name: "Save Persistent Query",
})
);
});

it("should add more filter blocks", async () => {
const { container } = render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

await userEvent.click(
await screen.findByRole("button", {
name: "Add Filter",
})
);
expect(
container.querySelector(`input[name="operator__and"]`)
).toBeChecked();
expect(
container.querySelector(`input[name="operator__or"]`)
).not.toBeChecked();

await userEvent.click(
screen.getAllByRole("option", {
name: "OR",
})[1]
);

await userEvent.type(
screen.getAllByLabelText("Field")[2],
"entity-1-number-field"
);
await userEvent.keyboard("{Enter}");

await userEvent.type(screen.getAllByLabelText("Operator")[2], "Is Null");
await userEvent.keyboard("{Enter}");

expect(screen.getAllByLabelText("Value")).toHaveLength(2);

await userEvent.click(
screen.getByRole("button", {
name: "Save Persistent Query",
})
);
});

it("should render current state correctly", async () => {
const { container } = render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

expect(
container.querySelector(`input[name="children[0].children[0].id"]`)
).toHaveValue("entity-1-string-field");

expect(
container.querySelector(
`input[name="children[0].children[0].value.operator"]`
)
).toHaveValue("e");

expect(
container.querySelector(
`input[name="children[0].children[0].value.value"]`
)
).toHaveValue("equal-value");

expect(
container.querySelector(`input[name="children[0].children[1].id"]`)
).toHaveValue("entity-1-date-field");

expect(
container.querySelector(
`input[name="children[0].children[1].value.operator"]`
)
).toHaveValue("l");

expect(
container.querySelector(
`input[name="children[0].children[1].value.value"]`
)
).toHaveValue("less-than-value");

expect(
container.querySelector(`input[name="children[0].operator__and"]`)
).not.toBeChecked();

expect(
container.querySelector(`input[name="children[0].operator__or"`)
).toBeChecked();

expect(
container.querySelector(`input[name="children[1].children[0].id"]`)
).toHaveValue("entity-1-number-field");

expect(
container.querySelector(
`input[name="children[1].children[0].value.operator"]`
)
).toHaveValue("s");

expect(
container.querySelector(
`input[name="children[1].children[0].value.value"]`
)
).toBeNull();

expect(
container.querySelector(`input[name="operator__and"]`)
).not.toBeChecked();
expect(container.querySelector(`input[name="operator__or"]`)).toBeChecked();
});

it("should remove all filters", async () => {
render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

await userEvent.click(
screen.getAllByRole("button", {
name: "Remove Nested Filter",
})[0]
);
await userEvent.click(
screen.getAllByRole("button", {
name: "Remove Nested Filter",
})[0]
);
await userEvent.click(
screen.getAllByRole("button", {
name: "Remove Nested Filter",
})[0]
);

expect(screen.queryByLabelText("Field")).not.toBeInTheDocument();
expect(screen.queryByLabelText("Value")).not.toBeInTheDocument();
expect(screen.queryByLabelText("Operator")).not.toBeInTheDocument();
expect(screen.queryByRole("option")).not.toBeInTheDocument();

await userEvent.click(
screen.getByRole("button", {
name: "Save Persistent Query",
})
);
});

it("should not show any filters", async () => {
render(
<ApplicationRoot>
<EntityPersistentQuerySettings />
</ApplicationRoot>
);

expect(screen.queryByLabelText("Field")).not.toBeInTheDocument();
expect(screen.queryByLabelText("Value")).not.toBeInTheDocument();
expect(screen.queryByLabelText("Operator")).not.toBeInTheDocument();
expect(screen.queryByRole("option")).not.toBeInTheDocument();
});
});
14 changes: 0 additions & 14 deletions src/__tests__/admin/[entity]/config/presentation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,4 @@ describe("pages/admin/[entity]/config/presentation", () => {
expect(screen.getByLabelText("Script")).toHaveValue("");
});
});

// it("should show the documentation panel", async () => {
// render(
// <ApplicationRoot>
// <EntityPresentationScriptSettings />
// </ApplicationRoot>
// );

// await userEvent.click(
// screen.getByRole("button", { name: "Explain Presentation Script" })
// );

// expect(screen.getByRole("dialog", { name: "Documentation" })).toBeVisible();
// });
});
Loading

0 comments on commit e6051a4

Please sign in to comment.