diff --git a/README.md b/README.md index 5d8c020..34c4026 100644 --- a/README.md +++ b/README.md @@ -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 buspark@bu.edu account. The api calls to typeform are handled in /api/retrieveQuestions + You can contact Jacob at jmstein@bu.edu for information if you need. diff --git a/code/src/pages/__tests__/ChoiceClick.test.js b/code/src/pages/__tests__/ChoiceClick.test.js new file mode 100644 index 0000000..57b3739 --- /dev/null +++ b/code/src/pages/__tests__/ChoiceClick.test.js @@ -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( + + ); + + await waitFor(() => fireEvent.click(screen.getByText('Choice 1'))); + expect(screen.getByText('Question 1')).toBeInTheDocument(); // New question based on the choice + }); +}); diff --git a/code/src/pages/__tests__/LocalStorage.test.js b/code/src/pages/__tests__/LocalStorage.test.js new file mode 100644 index 0000000..e61093d --- /dev/null +++ b/code/src/pages/__tests__/LocalStorage.test.js @@ -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 }); +}); diff --git a/code/testSetup.js b/code/testSetup.js new file mode 100644 index 0000000..34f5ae3 --- /dev/null +++ b/code/testSetup.js @@ -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 };