Skip to content

Commit

Permalink
Removing chai and sinon dependencies for tests. Refactored some tests…
Browse files Browse the repository at this point in the history
…. Partially resolves #3074
  • Loading branch information
alexcottner authored Nov 29, 2023
1 parent 8c1c786 commit 6cdba91
Show file tree
Hide file tree
Showing 42 changed files with 1,067 additions and 1,550 deletions.
366 changes: 2 additions & 364 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"@typescript-eslint/parser": "^5.0.0",
"babel-jest": "^29.7.0",
"babel-loader": "^9.1.0",
"chai": "^4.1.2",
"cross-env": "^7.0.2",
"css-loader": "^6.7.1",
"eslint": "^8.19.0",
Expand All @@ -83,7 +82,6 @@
"mini-css-extract-plugin": "^2.1.0",
"prettier": "^3.0.2",
"shx": "^0.3.2",
"sinon": "^15.0.0",
"style-loader": "^3.0.0",
"toctoc": "^0.4.0",
"typescript": "^5.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/components/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ export default function AuthForm({
serverChange();
};

const serverInfoCallback = auth => {
getServerInfo(auth);
const serverInfoCallback = async auth => {
await getServerInfo(auth);
setshowSpinner(false);
};

Expand Down
5 changes: 4 additions & 1 deletion src/components/signoff/SimpleReview/PerRecordDiffView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ function Diff({
}

return (
<div className={`record-diff card mb-4 ${className}`}>
<div
className={`record-diff card mb-4 ${className}`}
data-testid="record-diff"
>
<div className="card-header">
<DiffLabel changeType={changeType} /> {id}
</div>
Expand Down
2 changes: 1 addition & 1 deletion test/__mocks__/svgrMock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";
export default "SvgrURL";
const SvgrMock = React.forwardRef((props, ref) => (
<span ref={ref} {...props} />
Expand Down
128 changes: 67 additions & 61 deletions test/components/AuthForm_test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import { createSandbox, createComponent } from "../test_utils";
import { renderWithProvider } from "../test_utils";
import { DEFAULT_KINTO_SERVER } from "../../src/constants";
import { DEFAULT_SERVERINFO } from "../../src/reducers/session";
import { render, fireEvent, waitFor } from "@testing-library/react";
import * as React from "react";
import React from "react";
import AuthForm from "../../src/components/AuthForm";
import sinon from "sinon";

describe("AuthForm component", () => {
let sandbox;

beforeEach(() => {
jest.resetModules();
sandbox = createSandbox();
});

afterEach(() => {
sandbox.restore();
});
describe("Single server config option", () => {
it("should set the default server url in a visible field", () => {
const node = createComponent(
const node = renderWithProvider(
<AuthForm
session={{ authenticated: false, serverInfo: DEFAULT_SERVERINFO }}
/>
);

const element = node.querySelector("input[id='root_server']");
const element = node.queryByLabelText("Server*");
expect(element.type).toBe("text");
expect(element.value).toBe(DEFAULT_KINTO_SERVER);
});
Expand All @@ -39,11 +32,13 @@ describe("AuthForm component", () => {
serverChange;

beforeEach(() => {
setupSession = sandbox.spy();
serverChange = sandbox.spy();
getServerInfo = sandbox.spy();
navigateToExternalAuth = sandbox.spy();
navigateToOpenID = sandbox.spy();
setupSession = jest.fn();
serverChange = jest.fn();
(getServerInfo = async () => {
new Promise(resolve => setTimeout(resolve, 1000)); // simulate server response taking a second
}),
(navigateToExternalAuth = jest.fn());
navigateToOpenID = jest.fn();
const props = {
match: {},
setupSession,
Expand All @@ -70,32 +65,38 @@ describe("AuthForm component", () => {
},
},
};
node = createComponent(<AuthForm {...props} />);
node = renderWithProvider(<AuthForm {...props} />);
});

it("should render a setup form", () => {
expect(node.querySelector("form")).toBeDefined();
expect(node.getByTestId("formWrapper")).toBeDefined();
});

describe("Basic Auth", () => {
it("should submit setup data", async () => {
fireEvent.change(node.querySelector("#root_server"), {
fireEvent.change(node.queryByLabelText("Server*"), {
target: { value: "http://test.server/v1" },
});
expect(node.querySelector(".spinner")).toBeDefined();
await waitFor(() => new Promise(resolve => setTimeout(resolve, 500))); // debounce wait
expect(node.querySelector(".spinner")).toBeNull();
await node.findByTestId("spinner"); // spinner should show up
await waitFor(() => new Promise(resolve => setTimeout(resolve, 400))); // debounce wait
expect(node.queryByTestId("spinner")).toBeNull(); // spinner should be gone by now

fireEvent.click(node.querySelectorAll("[type=radio]")[1]);
fireEvent.change(node.querySelector("#root_credentials_username"), {
target: { value: "user" },
});
fireEvent.change(node.querySelector("#root_credentials_password"), {
target: { value: "pass" },
});
fireEvent.click(node.container.querySelectorAll("[type=radio]")[1]);
fireEvent.change(
node.container.querySelector("#root_credentials_username"),
{
target: { value: "user" },
}
);
fireEvent.change(
node.container.querySelector("#root_credentials_password"),
{
target: { value: "pass" },
}
);

fireEvent.submit(node.querySelector("form"));
sinon.assert.calledWithExactly(setupSession, {
fireEvent.submit(node.container.querySelector("form"));
expect(setupSession).toHaveBeenCalledWith({
server: "http://test.server/v1",
authType: "basicauth",
credentials: {
Expand All @@ -109,19 +110,25 @@ describe("AuthForm component", () => {

describe("LDAP", () => {
it("should submit setup data", async () => {
fireEvent.change(node.querySelector("#root_server"), {
fireEvent.change(node.queryByLabelText("Server*"), {
target: { value: "http://test.server/v1" },
});
await waitFor(() => new Promise(resolve => setTimeout(resolve, 500))); // debounce wait
fireEvent.click(node.querySelectorAll("[type=radio]")[3]);
fireEvent.change(node.querySelector("#root_credentials_username"), {
target: { value: "[email protected]" },
});
fireEvent.change(node.querySelector("#root_credentials_password"), {
target: { value: "pass" },
});
fireEvent.submit(node.querySelector("form"));
sinon.assert.calledWithExactly(setupSession, {
fireEvent.click(node.container.querySelectorAll("[type=radio]")[3]);
fireEvent.change(
node.container.querySelector("#root_credentials_username"),
{
target: { value: "[email protected]" },
}
);
fireEvent.change(
node.container.querySelector("#root_credentials_password"),
{
target: { value: "pass" },
}
);
fireEvent.submit(node.container.querySelector("form"));
expect(setupSession).toHaveBeenCalledWith({
server: "http://test.server/v1",
authType: "ldap",
credentials: {
Expand All @@ -135,14 +142,14 @@ describe("AuthForm component", () => {

describe("FxA", () => {
it("should navigate to external auth URL", async () => {
fireEvent.change(node.querySelector("#root_server"), {
fireEvent.change(node.queryByLabelText("Server*"), {
target: { value: "http://test.server/v1" },
});
await waitFor(() => new Promise(resolve => setTimeout(resolve, 500))); // debounce wait
fireEvent.click(node.querySelectorAll("[type=radio]")[2]);
fireEvent.change(node.querySelector("form"));
fireEvent.submit(node.querySelector("form"));
sinon.assert.calledWithExactly(navigateToExternalAuth, {
fireEvent.click(node.container.querySelectorAll("[type=radio]")[2]);
fireEvent.change(node.container.querySelector("form"));
fireEvent.submit(node.container.querySelector("form"));
expect(navigateToExternalAuth).toHaveBeenCalledWith({
server: "http://test.server/v1",
authType: "fxa", // fxa = credentials omitted
redirectURL: undefined,
Expand All @@ -152,14 +159,13 @@ describe("AuthForm component", () => {

describe("OpenID", () => {
it("should navigate to external auth URL", async () => {
fireEvent.change(node.querySelector("#root_server"), {
fireEvent.change(node.queryByLabelText("Server*"), {
target: { value: "http://test.server/v1" },
});
await waitFor(() => new Promise(resolve => setTimeout(resolve, 500))); // debounce wait
fireEvent.click(node.querySelectorAll("[type=radio]")[4]);
fireEvent.submit(node.querySelector("form"));
sinon.assert.calledWithExactly(
navigateToOpenID,
fireEvent.click(node.container.querySelectorAll("[type=radio]")[4]);
fireEvent.submit(node.container.querySelector("form"));
expect(navigateToOpenID).toHaveBeenCalledWith(
{
server: "http://test.server/v1",
redirectURL: undefined,
Expand All @@ -175,38 +181,38 @@ describe("AuthForm component", () => {
it("should set the server field value using a default value if there's no servers", () => {
const props = {
match: {},
serverChange: sandbox.spy(),
getServerInfo: sandbox.spy(),
serverChange: jest.fn(),
getServerInfo: jest.fn(),
servers: [],
session: { authenticated: false, serverInfo: DEFAULT_SERVERINFO },
};
const node = createComponent(<AuthForm {...props} />);
const node = renderWithProvider(<AuthForm {...props} />);

expect(node.querySelector("#root_server").value).toBe(
expect(node.queryByLabelText("Server*").value).toBe(
"https://demo.kinto-storage.org/v1/"
);
});

it("should set the server field value using latest entry from servers", () => {
const props = {
match: {},
serverChange: sandbox.spy(),
getServerInfo: sandbox.spy(),
serverChange: jest.fn(),
getServerInfo: jest.fn(),
servers: [{ server: "http://server.test/v1", authType: "anonymous" }],
session: { authenticated: false, serverInfo: DEFAULT_SERVERINFO },
};
const node = createComponent(<AuthForm {...props} />);
const node = renderWithProvider(<AuthForm {...props} />);

expect(node.querySelector("#root_server").value).toBe(
expect(node.queryByLabelText("Server*").value).toBe(
"http://server.test/v1"
);
});

it("should set the authType field value using latest entry from servers history for that server", async () => {
const props = {
match: {},
serverChange: sandbox.spy(),
getServerInfo: sandbox.spy(),
serverChange: jest.fn(),
getServerInfo: jest.fn(),
servers: [
{ server: "http://server.test/v1", authType: "basicauth" },
{ server: "http://test.server/v1", authType: "openid-google" },
Expand Down
2 changes: 1 addition & 1 deletion test/components/CollectionRecords_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderWithProvider } from "../test_utils";
import CollectionRecords from "../../src/components/collection/CollectionRecords";
import * as React from "react";
import React from "react";

describe("CollectionRecords component", () => {
const capabilities = {
Expand Down
Loading

0 comments on commit 6cdba91

Please sign in to comment.