-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from BU-Spark/sp24-dev
Add Tests to Original Dev
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |