Skip to content

Commit

Permalink
Merge pull request #830 from boxwise/bugfix/boxview-boxedit-tests
Browse files Browse the repository at this point in the history
BoxView: Fix Tests and Update GraphQL Error Policy for BoxView Query
  • Loading branch information
pylipp authored Jun 30, 2023
2 parents bfcebc4 + 97a8eeb commit 35f4280
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
55 changes: 47 additions & 8 deletions react/src/views/Box/BoxView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GraphQLError } from "graphql";
import "@testing-library/jest-dom";
import { screen, render, waitFor } from "tests/test-utils";
import userEvent from "@testing-library/user-event";
import { cache } from "queries/cache";
import BTBox, {
UPDATE_NUMBER_OF_ITEMS_IN_BOX_MUTATION,
UPDATE_STATE_IN_BOX_MUTATION,
Expand Down Expand Up @@ -44,6 +45,21 @@ const initialQuery = {
},
};

const initialQueryForChangeNumberOfBoxes = {
request: {
query: BOX_BY_LABEL_IDENTIFIER_AND_ALL_SHIPMENTS_QUERY,
variables: {
labelIdentifier: "1235",
},
},
result: {
data: {
box: generateMockBox({ labelIdentifier: "1235", numberOfItems: 31 }),
shipments: null,
},
},
};

const initialQueryForBoxInLegacyLostLocation = {
request: {
query: BOX_BY_LABEL_IDENTIFIER_AND_ALL_SHIPMENTS_QUERY,
Expand Down Expand Up @@ -123,13 +139,13 @@ const updateNumberOfItemsMutation = {
request: {
query: UPDATE_NUMBER_OF_ITEMS_IN_BOX_MUTATION,
variables: {
boxLabelIdentifier: "123",
boxLabelIdentifier: "1235",
numberOfItems: 32,
},
},
result: {
data: {
updateBox: generateMockBox({ numberOfItems: 32 }),
updateBox: generateMockBox({ numberOfItems: 32, labelIdentifier: "1235" }),
shipments: null,
},
},
Expand Down Expand Up @@ -395,8 +411,9 @@ it("3.1.2 - Change Number of Items", async () => {
const user = userEvent.setup();
render(<BTBox />, {
routePath: "/bases/:baseId/boxes/:labelIdentifier",
initialUrl: "/bases/2/boxes/123",
mocks: [initialQuery, updateNumberOfItemsMutation],
initialUrl: "/bases/2/boxes/1235",
mocks: [initialQueryForChangeNumberOfBoxes, updateNumberOfItemsMutation],
cache,
addTypename: true,
globalPreferences: {
dispatch: jest.fn(),
Expand All @@ -407,7 +424,7 @@ it("3.1.2 - Change Number of Items", async () => {
},
});

const title = await screen.findByRole("heading", { name: "Box 123" });
const title = await screen.findByRole("heading", { name: "Box 1235" });
expect(title).toBeInTheDocument();

expect(screen.getByRole("heading", { name: /31x snow trousers/i }));
Expand All @@ -427,15 +444,34 @@ it("3.1.2 - Change Number of Items", async () => {
await user.type(screen.getByRole("spinbutton"), "{backspace}");
await user.type(screen.getByRole("spinbutton"), "-");
await waitFor(() => expect(screen.getByRole("spinbutton")).toHaveValue("-"));
await user.click(screen.getByText(/Submit/i));

await user.click(
screen.getByRole("button", {
name: /submit/i,
}),
);

await waitFor(() => expect(screen.getByRole("spinbutton")).toHaveValue("0"));

// // // Test case 3.1.2.2 - Number of Item Validation
await user.type(screen.getByRole("spinbutton"), "{backspace}");
await user.type(screen.getByRole("spinbutton"), "1");
await waitFor(() => expect(screen.getByRole("spinbutton")).toHaveValue("1"));
await user.click(screen.getByText(/Submit/i));
expect(screen.getByText(/32x snow trousers/i));

await user.click(
screen.getByRole("button", {
name: /submit/i,
}),
);

await waitFor(() =>
expect(mockedCreateToast).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringMatching(/successfully added 1 items to box/i),
}),
),
);
expect(screen.getByTestId("boxview-number-items")).toHaveTextContent(/32x snow trousers/i);
}, 10000);

// Test case 3.1.3.1
Expand All @@ -445,6 +481,7 @@ it("3.1.3.1 - Change State to Scrap", async () => {
routePath: "/bases/:baseId/boxes/:labelIdentifier",
initialUrl: "/bases/2/boxes/123",
mocks: [initialQuery, updateBoxStateToScrapMutation, updateBoxStateToLostMutation],
cache,
addTypename: true,
globalPreferences: {
dispatch: jest.fn(),
Expand Down Expand Up @@ -477,6 +514,7 @@ it("3.1.3.2 - Change State to Lost", async () => {
initialUrl: "/bases/2/boxes/123",
mocks: [initialQuery, updateBoxStateToLostMutation],
addTypename: true,
cache,
globalPreferences: {
dispatch: jest.fn(),
globalPreferences: {
Expand Down Expand Up @@ -512,6 +550,7 @@ it("3.1.4 - Move location", async () => {
routePath: "/bases/:baseId/boxes/:labelIdentifier",
initialUrl: "/bases/1/boxes/125",
mocks: [initialQueryMoveLocationOfBox, moveLocationOfBoxMutation],
cache,
addTypename: true,
globalPreferences: {
dispatch: jest.fn(),
Expand Down
3 changes: 2 additions & 1 deletion react/src/views/Box/BoxView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function BTBox() {
const allData = useQuery<BoxByLabelIdentifierQuery, BoxByLabelIdentifierQueryVariables>(
BOX_BY_LABEL_IDENTIFIER_AND_ALL_SHIPMENTS_QUERY,
{
errorPolicy: "all",
variables: {
labelIdentifier,
},
Expand Down Expand Up @@ -210,7 +211,7 @@ function BTBox() {
updateNumberOfItemsMutationStatus.loading;

const error =
allData.error ||
(allData.error && allData?.data === undefined) ||
assignBoxToDistributionEventMutationStatus.error ||
unassignBoxFromDistributionEventMutationStatus.error;

Expand Down

0 comments on commit 35f4280

Please sign in to comment.