From 7122bf1b3d7da0ee63786153a97f8e7b20924c76 Mon Sep 17 00:00:00 2001 From: Chris Harris <94482802+chrisharris02@users.noreply.github.com> Date: Mon, 6 May 2024 18:18:24 -0400 Subject: [PATCH 1/2] Adding typeform sentence. --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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. From d290db17e3a8b95d188c4f152db9012997650863 Mon Sep 17 00:00:00 2001 From: Marek Pinto Date: Mon, 6 May 2024 21:44:10 -0400 Subject: [PATCH 2/2] Add Test Code --- code/src/pages/__tests__/ChoiceClick.test.js | 47 +++++++++++++++++++ code/src/pages/__tests__/LocalStorage.test.js | 7 +++ code/testSetup.js | 35 ++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 code/src/pages/__tests__/ChoiceClick.test.js create mode 100644 code/src/pages/__tests__/LocalStorage.test.js create mode 100644 code/testSetup.js 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 };