-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ZEVA-1744: Unit tests for 3 supplementary components #2375
base: release-1.65.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,30 @@ | ||
module.exports = { | ||
testEnvironment: 'jest-environment-jsdom', | ||
collectCoverageFrom: ['src/**/*.{js,jsx}'], | ||
coverageReporters: ['json'], | ||
testEnvironment: "jest-environment-jsdom", | ||
collectCoverageFrom: ["src/**/*.{js,jsx}"], | ||
coverageReporters: ["json", "html", "lcov"], | ||
coverageThreshold: { | ||
global: { | ||
branches: 1, | ||
functions: 1, | ||
lines: 1, | ||
statements: -6000 | ||
} | ||
statements: -6000, | ||
}, | ||
}, | ||
moduleFileExtensions: ['js', 'node', 'json'], | ||
moduleFileExtensions: ["js", "node", "json"], | ||
moduleNameMapper: { | ||
'^.+\\.(css|less|scss)$': '<rootDir>/__mocks__/style.js' | ||
"^.+\\.(css|less|scss)$": "<rootDir>/__mocks__/style.js", | ||
}, | ||
setupFiles: ['./jest.setup.js'], | ||
setupFiles: ["./jest.setup.js"], | ||
testEnvironmentOptions: { | ||
url: 'http://localhost/' | ||
url: "http://localhost/", | ||
}, | ||
transform: { | ||
'^.+\\.(js|jsx)$': 'babel-jest' | ||
"^.+\\.(js|jsx)$": "babel-jest", | ||
}, | ||
coveragePathIgnorePatterns: ['node_modules/'], | ||
testPathIgnorePatterns: ["/node_modules/", "/__tests__/test-data/"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can have |
||
coveragePathIgnorePatterns: ["node_modules/", "/__tests__/test-data/"], | ||
verbose: true, | ||
globals: { | ||
__VERSION__: '' | ||
} | ||
} | ||
__VERSION__: "", | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { library } from '@fortawesome/fontawesome-svg-core' | ||
import { fab } from '@fortawesome/free-brands-svg-icons' | ||
import { far } from '@fortawesome/free-regular-svg-icons' | ||
import { fas } from '@fortawesome/free-solid-svg-icons' | ||
import { library } from "@fortawesome/fontawesome-svg-core"; | ||
import { fab } from "@fortawesome/free-brands-svg-icons"; | ||
import { far } from "@fortawesome/free-regular-svg-icons"; | ||
import { fas } from "@fortawesome/free-solid-svg-icons"; | ||
|
||
library.add(fab, far, fas) | ||
library.add(fab, far, fas); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,8 @@ | |
"@storybook/addon-storyshots": "^6.1.21", | ||
"@storybook/react": "^6.1.21", | ||
"@testing-library/jest-dom": "^4.2.4", | ||
"@testing-library/react": "^9.4.0", | ||
"@testing-library/react": "^10.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upgraded version to get some more utility; however, could not upgrade to latest due to being on React v16 |
||
"@testing-library/user-event": "^14.5.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good for testing uploads |
||
"babel-eslint": "^7.2.3", | ||
"babel-jest": "^24.9.0", | ||
"babel-loader": "^8.1.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import SupplementaryAlert from "../SupplementaryAlert"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import { SupplimentaryAlertsTestData } from "./test-data/testData"; | ||
|
||
// mock the alert component | ||
jest.mock( | ||
"../../../app/components/Alert", | ||
() => | ||
({ title, icon, classname, message }) => ( | ||
<div data-testid="alert-mock"> | ||
<span data-testid="title">{title}</span> | ||
<span data-testid="icon">{icon}</span> | ||
<span data-testid="classname">{classname}</span> | ||
<span data-testid="message">{message}</span> | ||
</div> | ||
), | ||
); | ||
|
||
describe("SupplementaryAlert", () => { | ||
const { testCases } = SupplimentaryAlertsTestData; | ||
const setup = (status) => { | ||
render(<SupplementaryAlert {...defaultProps} status={status} />); | ||
}; | ||
|
||
const defaultProps = { | ||
date: "2024-12-10", | ||
user: "John Doe", | ||
}; | ||
|
||
const checkStatusRender = (title, icon, classname, message) => { | ||
expect(screen.getByTestId("title").textContent).toBe(title); | ||
expect(screen.getByTestId("icon").textContent).toBe(icon); | ||
expect(screen.getByTestId("classname").textContent).toBe(classname); | ||
expect(screen.getByTestId("message").textContent).toBe(message); | ||
}; | ||
|
||
testCases.forEach(({ status, expected }) => { | ||
it(`renders correctly for status "${status}"`, () => { | ||
setup(status); | ||
checkStatusRender( | ||
expected.title, | ||
expected.icon, | ||
expected.classname, | ||
expected.message, | ||
); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React from "react"; | ||
import { render, screen, fireEvent, cleanup } from "@testing-library/react"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import SupplierInformation from "../SupplierInformation"; | ||
import { SupplierInfoTestData } from "./test-data/testData"; | ||
|
||
describe("SupplierInformation component", () => { | ||
const mockHandleInputChange = jest.fn(); | ||
const { mockDetails, mockNewData, mockUser } = SupplierInfoTestData; | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const defaultProps = { | ||
details: mockDetails, | ||
handleInputChange: mockHandleInputChange, | ||
newData: mockNewData, | ||
user: mockUser, | ||
isEditable: true, | ||
}; | ||
|
||
const setup = () => { | ||
render(<SupplierInformation {...defaultProps} />); | ||
}; | ||
|
||
it("displays initial values", () => { | ||
setup(); | ||
expect(screen.getByText("Test Company")).toBeInTheDocument(); | ||
// address used in multiple places | ||
expect( | ||
screen.getAllByText("123 Main St, Suite 100, Surrey, BC, Canada, V0S1N0"), | ||
).not.toHaveLength(0); | ||
expect( | ||
screen.getAllByText("456 Elm St, Victoria, BC, Canada, V8V1V1"), | ||
).not.toHaveLength(0); | ||
expect(screen.getByText("Ford")).toBeInTheDocument(); | ||
expect(screen.getByText("Toyota")).toBeInTheDocument(); | ||
expect(screen.getByText("Class A")).toBeInTheDocument(); | ||
}); | ||
|
||
it("applies the highlight class when values differ", () => { | ||
setup(); | ||
expect(screen.getByDisplayValue("New Test Company")).toHaveClass( | ||
"highlight", | ||
); | ||
expect(screen.getByDisplayValue("Class B")).toHaveClass("highlight"); | ||
}); | ||
|
||
it("does not apply the highlight class when values match", () => { | ||
const props = { | ||
...defaultProps, | ||
newData: { | ||
supplierInfo: { | ||
...SupplierInfoTestData.mockNewData.supplierInfo, | ||
legalName: "Test Company", | ||
supplierClass: "Class A", | ||
}, | ||
}, | ||
}; | ||
|
||
render(<SupplierInformation {...props} />); | ||
|
||
expect(screen.getByDisplayValue("Test Company")).not.toHaveClass( | ||
"highlight", | ||
); | ||
expect(screen.getByDisplayValue("Class A")).not.toHaveClass("highlight"); | ||
}); | ||
|
||
it("calls handleInputChange on input change", () => { | ||
setup(); | ||
const input = screen.getByDisplayValue("New Test Company"); | ||
fireEvent.change(input, { target: { value: "Updated Company" } }); | ||
|
||
expect(mockHandleInputChange).toHaveBeenCalledTimes(1); | ||
}); | ||
it("renders a message for non-government users", () => { | ||
setup(); | ||
expect( | ||
screen.getByText( | ||
"Make the required updates in the fields next to the original submitted values and provide an explanation in the comment box at the bottom of this form.", | ||
), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("does not render the message for government users", () => { | ||
const props = { | ||
...defaultProps, | ||
user: { isGovernment: true }, | ||
}; | ||
render(<SupplierInformation {...props} />); | ||
expect( | ||
screen.queryByText( | ||
"Make the required updates in the fields next to the original submitted values and provide an explanation in the comment box at the bottom of this form.", | ||
), | ||
).not.toBeInTheDocument(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get visual representation of code coverage on local