Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e115100
ignore storybook static stuff
omeryar Nov 9, 2022
0799fbd
ignore storybook static
omeryar Nov 16, 2022
c885839
typescript packages
omeryar Nov 29, 2022
7491d5a
default next config (but with strict=true)
omeryar Nov 29, 2022
4c7ce0a
turn _app to TS
omeryar Nov 29, 2022
5853f55
turn the home page into TS
omeryar Nov 29, 2022
c967061
util library in TS
omeryar Nov 29, 2022
e35d53a
API cpnverted to TS
omeryar Nov 29, 2022
c61cb34
Calculator component converted to TS
omeryar Nov 29, 2022
8a1e704
hadnle case where ref value is null
omeryar Dec 9, 2022
0d6f164
install and set up jest
omeryar Dec 9, 2022
2b37102
example of testing a standalone library
omeryar Dec 9, 2022
2588bae
example of testing the API with mocks
omeryar Dec 9, 2022
4a8fd0f
example of testing a component with react testing lib
omeryar Dec 9, 2022
6011167
example of testing a page
omeryar Dec 9, 2022
7f5a692
add handling for non-number params in add method
omeryar Dec 9, 2022
b8764cc
added typescript
Dec 15, 2022
a6ff9bc
Merge pull request #1 from dkprioleau/refactor-to-typescript
dkprioleau Dec 15, 2022
e4fbb70
alternative way of handling form values
omeryar Dec 15, 2022
65ef772
installed playwright package
omeryar Dec 15, 2022
9be2073
make sure jest doesn't run playwright tests
omeryar Dec 15, 2022
7b519f4
playwright config file
omeryar Dec 15, 2022
ffdf9bd
basic api test
omeryar Dec 15, 2022
6e1405c
added id to result element
omeryar Dec 15, 2022
29e2918
add basic tests for home page
omeryar Dec 15, 2022
69f53ba
add coverage printing
omeryar Dec 15, 2022
2acd592
storybook installation (minus main and preview files)
omeryar Dec 21, 2022
80d8bd6
stroybook main and preview files
omeryar Dec 21, 2022
95a61ef
WIP story for testing home page
omeryar Dec 21, 2022
4204cf0
added typescript dependency
Dec 29, 2022
2f91c5b
refactored errors
Jan 3, 2023
8eddb82
Merge pull request #2 from dkprioleau/refactor-to-typescript
dkprioleau Jan 3, 2023
4598509
Merge remote-tracking branch 'upstream/main'
Jan 4, 2023
c529198
testing
Jan 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# playwright
/playwright-report/
/playwright/.cache/
/test-results/
/storybook-static/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
legacy-peer-deps=true
20 changes: 20 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
stories: [
"../stories/**/*.stories.mdx",
"../stories/**/*.stories.@(js|jsx|ts|tsx)",
],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"storybook-addon-next",
],
framework: "@storybook/react",
core: {
builder: "@storybook/builder-webpack5",
},
features: {
interactionsDebugger: true,
},
};

29 changes: 29 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "../styles/globals.css";
import { ThemeProvider, createTheme } from "@mui/material/styles";

const darkTheme = createTheme({
palette: {
mode: "dark",
},
});

const withThemeProvider = (Story, context) => {
return (
<ThemeProvider theme={darkTheme}>
<Story {...context} />
</ThemeProvider>
);
};

export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};

export const decorators = [withThemeProvider];

39 changes: 39 additions & 0 deletions __tests__/api/calculate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createMocks } from "node-mocks-http";
import handler from "../../pages/api/calculate/[...params]";

describe("test calculate API handler", () => {
function getMockHttpObjs(method, query) {
const { req, res } = createMocks({ method, query });
return { req, res };
}

it("returns a valid result and status code when add is successful", () => {
const { req, res } = getMockHttpObjs("GET", { params: ["add", 1, 1] });
handler(req, res);

expect(res.statusCode).toBe(200);
expect(res.getHeaders()).toEqual({ "content-type": "application/json" });
expect(res._getJSONData()).toEqual({ result: 2 });
});

it("should only accept GET method", () => {
let req, res;

({ req, res } = getMockHttpObjs("POST", { params: ["add", 1, 1] }));
handler(req, res);
expect(res.statusCode).toBe(500);

({ req, res } = getMockHttpObjs("PUT", { params: ["add", 1, 1] }));
handler(req, res);
expect(res.statusCode).toBe(500);

({ req, res } = getMockHttpObjs("DELETE", { params: ["add", 1, 1] }));
handler(req, res);
expect(res.statusCode).toBe(500);

({ req, res } = getMockHttpObjs("WHATEVER", { params: ["add", 1, 1] }));
handler(req, res);
expect(res.statusCode).toBe(500);
});
});

89 changes: 89 additions & 0 deletions __tests__/components/calculator.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
render,
screen,
fireEvent,
cleanup,
waitFor,
} from "@testing-library/react";
import "@testing-library/jest-dom";
import userEvent from "@testing-library/user-event";
import Calculator from "../../components/Calculator";

//sets up userEvent to reuse it through the test
//const user = userEvent.setup()

describe("Calculator component testing", () => {
const component = render(<Calculator />);
it("has all the fields", () => {
expect(document.getElementById("first")).toBeInTheDocument();
expect(document.getElementById("second")).toBeInTheDocument();
expect(document.getElementById("operation")).toBeInTheDocument();
expect(document.querySelector("button[type=submit]")).toBeInTheDocument();
});
});

// Feature#2 Prevent Double Form Submission TESTs

describe.only("Disable Submit Button", () => {
beforeEach(() => {
render(<Calculator />);
});
afterEach(cleanup);

it("Should disable calculate button on click", async () => {

const inputFieldFirst: HTMLInputElement =
screen.getByLabelText(/First Number/i);
await userEvent.type(inputFieldFirst, "2");

const inputFieldSecond: HTMLInputElement =
screen.getByLabelText(/Second Number/i);
await userEvent.type(inputFieldSecond, "3");

const operations: HTMLElement | null = document.getElementById("operation");

if (operations) {
fireEvent.change(operations, {
target: { value: "add" },
});
}
const form: HTMLElement | null = document.getElementById("calculator-form");
const submitButton: HTMLElement | null = document.querySelector("button[type=submit]");
if (form) {
await fireEvent.submit(form);
}
expect(submitButton).toBeDisabled();
});
});

it.only("Should give us the result after submit", async () => {
const timer = jest.useRealTimers();
const inputFieldFirst: HTMLInputElement =
screen.getByLabelText(/First Number/i);
await userEvent.type(inputFieldFirst, "2");

const inputFieldSecond: HTMLInputElement =
screen.getByLabelText(/Second Number/i);
await userEvent.type(inputFieldSecond, "3");

const operations: HTMLElement | null = document.getElementById("operation");

if (operations) {
fireEvent.change(operations, {
target: { value: "add" },
});
}
const submitButton: HTMLElement | null = document.querySelector("button[type=submit]");
if (submitButton) {
await fireEvent.click(submitButton);
}
//await waitFor(async () => await expect(submitButton).toBeDisabled())
//jest.useFakeTimers();
// expect(document.getElementById("result")).toHaveTextContent("5")

});


//expect(submitButton).toHaveAttribute('disabled')
// await userEvent.click(document.querySelector("button[type=submit]"))
// expect(document.getElementById("result")).toHaveTextContent("3")
12 changes: 12 additions & 0 deletions __tests__/pages/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";

import Home from "../../pages/index";

describe("Testing home page", () => {
it("has the header", () => {
render(<Home />);
const heading = screen.getByRole("heading", { level: 2 });
expect(heading.textContent).toBe("The Amazing Calculator");
});
});
24 changes: 24 additions & 0 deletions __tests__/utils/calculate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { add, subtract, divide, multiply } from "../../utils/calculate";

describe("basic library functionality", () => {
it("can add two numbers", () => {
expect(add(1, 1)).toBe(2);
expect(add(1, -1)).toBe(0);
expect(add(1.5, 1.5)).toBe(3);
});

it("doesn't like strings", () => {
expect(() => {
add(1, "a");
}).toThrow();

expect(() => {
add("a", 1);
}).toThrow();

expect(() => {
add(1, null);
}).toThrow();
});
});

54 changes: 54 additions & 0 deletions calculate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// import { add, subtract, divide, multiply } from "../../utils/calculate";

// describe("basic library functionality", () => {
// it("can add two numbers", () => {
// expect(add(1, 1)).toBe(2);
// expect(add(1, -1)).toBe(0);
// expect(add(1.5, 1.5)).toBe(3);
// });

// it("doesn't like strings", () => {
// expect(() => {
// add(1, "a");
// }).toThrow();

// expect(() => {
// add("a", 1);
// }).toThrow();

// expect(() => {
// add(1, null);
// }).toThrow();
// });
// });




// import { add, subtract, multiply, divide } from "../../utils/calculate";

/*
you can use test or it
first param gives a description of what you're testing
second param is function that gets called to run test
-inside func is our expected result
describe groups test
*/



// decscribe("testing library",
// () => {
// test("should add two numbers", () => {
// expect(add(1, 2).toBe(3));
// });
// test("should subtract two numbers", () => {
// expect(subtract(5, 3).toBe(2));
// });
// test("should multiply two numbers", () => {
// expect(multiply(5, 7).toBe(35));
// });
// test("should divide two numbers", () => {
// expect(divide(24, 12).toBe(2));
// });
// });
100 changes: 0 additions & 100 deletions components/Calculator.js

This file was deleted.

Loading