Skip to content

Commit 0c1289c

Browse files
committed
Set up Unit Tests
1 parent 79f091b commit 0c1289c

File tree

12 files changed

+4319
-242
lines changed

12 files changed

+4319
-242
lines changed

.github/workflows/testing.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Run Tests
2+
on: push
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Install modules
9+
run: yarn
10+
- name: Run tests
11+
run: yarn test
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from "react";
2+
import { render, screen, fireEvent } from "@testing-library/react";
3+
import { Provider } from "react-redux";
4+
import { BrowserRouter as Router } from "react-router-dom";
5+
import configureStore from "redux-mock-store";
6+
import { UserMenu } from "../src/components/UserMenu";
7+
import "@testing-library/jest-dom";
8+
9+
const mockStore = configureStore([]);
10+
const store = mockStore({});
11+
12+
describe("UserMenu Component", () => {
13+
beforeEach(() => {
14+
localStorage.setItem(
15+
"userInfo",
16+
JSON.stringify({
17+
user: {
18+
id: "123",
19+
user_metadata: {
20+
username: "John Doe",
21+
22+
},
23+
},
24+
})
25+
);
26+
});
27+
28+
afterEach(() => {
29+
localStorage.clear();
30+
});
31+
32+
test("renders user menu with username and avatar", () => {
33+
render(
34+
<Provider store={store}>
35+
<Router>
36+
<UserMenu />
37+
</Router>
38+
</Provider>
39+
);
40+
41+
expect(screen.getByText("John Doe")).toBeInTheDocument();
42+
expect(screen.getByText("JD")).toBeInTheDocument();
43+
});
44+
45+
test("opens edit account dialog", () => {
46+
render(
47+
<Provider store={store}>
48+
<Router>
49+
<UserMenu />
50+
</Router>
51+
</Provider>
52+
);
53+
54+
fireEvent.click(screen.getByText("John Doe"));
55+
fireEvent.click(screen.getByText("Edit Account"));
56+
57+
expect(screen.getByText("Edit Account")).toBeInTheDocument();
58+
expect(screen.getByLabelText("New User Name")).toBeInTheDocument();
59+
});
60+
61+
test("opens delete account dialog", () => {
62+
render(
63+
<Provider store={store}>
64+
<Router>
65+
<UserMenu />
66+
</Router>
67+
</Provider>
68+
);
69+
70+
fireEvent.click(screen.getByText("John Doe"));
71+
fireEvent.click(screen.getByText("Delete Account"));
72+
73+
expect(screen.getByText("Delete Account")).toBeInTheDocument();
74+
expect(
75+
screen.getByText("Are you sure you want to delete your account? This action cannot be undone.")
76+
).toBeInTheDocument();
77+
});
78+
79+
test("logs out user", () => {
80+
render(
81+
<Provider store={store}>
82+
<Router>
83+
<UserMenu />
84+
</Router>
85+
</Provider>
86+
);
87+
88+
fireEvent.click(screen.getByText("John Doe"));
89+
fireEvent.click(screen.getByText("Logout"));
90+
91+
// Add assertions to check if the user is logged out
92+
});
93+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {sum} from '../src/index'
2+
import { describe, test, expect } from "@jest/globals"
3+
4+
describe('Sum function', () =>{
5+
test('Returns correct value', () =>{
6+
expect(sum(2, 3)).toEqual(5)
7+
})
8+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Config } from 'jest';
2+
3+
const config: Config = {
4+
preset: 'ts-jest',
5+
moduleNameMapper: {
6+
'\\.(css|scss)$': 'identity-obj-proxy',
7+
"^.+\\.svg": "<rootDir>/tests/mocks/svgMock.tsx"
8+
},
9+
// to obtain access to the matchers.
10+
setupFilesAfterEnv: ['<rootDir>/tests/setupTests.ts'],
11+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
12+
modulePaths: ['<rootDir>'],
13+
testEnvironment: 'jsdom',
14+
transform: {
15+
'^.+\\.(ts|tsx)$': ['ts-jest', {
16+
tsconfig: 'tsconfig.test.json',
17+
}],
18+
'^.+\\.(js|jsx)$': 'babel-jest',
19+
},
20+
};
21+
22+
export default config;

0 commit comments

Comments
 (0)