Skip to content

Commit

Permalink
Merge pull request #68 from BU-Spark/sp24-dev
Browse files Browse the repository at this point in the history
Add Tests to Original Dev
  • Loading branch information
DarianC26 authored May 7, 2024
2 parents 52b5095 + da8aef9 commit bceacbf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
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 };

0 comments on commit bceacbf

Please sign in to comment.