Skip to content
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

Add Tests to Original Dev #68

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,6 @@ $ npm run test
We deployed the frontend in [vercel](https://se-bch-als-resource-app-zeta.vercel.app/)
Please note the deployed link does not work on the BU Network for some reason.

The data is hosted on Typeform on the [email protected] account. The api calls to typeform are handled in /api/retrieveQuestions

You can contact Jacob at [email protected] for information if you need.
47 changes: 47 additions & 0 deletions code/src/pages/__tests__/ChoiceClick.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { fetchMock } from '../../../testSetup'; // Assuming fetchMock setup is done in testSetup
import CommunicationPage from '../communication';
import { FocusedBookmarkProvider } from '../../contexts/FocusedBookmarkContext';
import '@testing-library/jest-dom';

// Mocking useRouter from next/router
jest.mock('next/router', () => ({
useRouter() {
return {
route: '/',
pathname: '/',
query: {},
asPath: '/',
push: jest.fn(), // Mock push to assert navigation
replace: jest.fn(),
reload: jest.fn(),
back: jest.fn(),
prefetch: jest.fn(),
beforePopState: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
},
};
},
}));

fetchMock.enableMocks();

describe('CommunicationPage Tests', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

test('clicking a choice updates the UI correctly', async () => {
fetchMock.mockResponseOnce(JSON.stringify({ questions: [{ ref: "ref1", title: "Question 1", choices: [{ id: "1", label: "Choice 1", link: "nextRef" }] }] }));

render(<FocusedBookmarkProvider>
<CommunicationPage />
</FocusedBookmarkProvider>);

await waitFor(() => fireEvent.click(screen.getByText('Choice 1')));
expect(screen.getByText('Question 1')).toBeInTheDocument(); // New question based on the choice
});
});
7 changes: 7 additions & 0 deletions code/src/pages/__tests__/LocalStorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { localStorageMock } from '../../../testSetup';

test('should save and load data from local storage', () => {
localStorageMock.setItem('testKey', JSON.stringify({ id: 123 }));
const item = JSON.parse(localStorageMock.getItem('testKey'));
expect(item).toEqual({ id: 123 });
});
35 changes: 35 additions & 0 deletions code/testSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fetchMock from 'jest-fetch-mock';

fetchMock.enableMocks();

const mockedRouter = {
push: jest.fn(),
route: '/communication',
pathname: '',
query: '',
asPath: '',
};

const localStorageMock = (function() {
let store = {};
return {
getItem: function(key) {
return store[key] || null;
},
setItem: function(key, value) {
store[key] = value.toString();
},
clear: function() {
store = {};
},
removeItem: function(key) {
delete store[key];
},
};
})();

Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});

export { fetchMock, mockedRouter, localStorageMock };
Loading