-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Unit testing and add to CI (#58)
* chore: gitignore coverage reports * chore: update jest test script * refactor: minimize error logs * test: update thorough user controller testing * chore: update tsconfig type files * test: add auth middleware tests * test: add poll tests * test: improve test coverage * ci: add server unit testing to ci * test: update frontend unit tests * ci: add frontend unit tests to ci
- Loading branch information
1 parent
dcfd215
commit cd5f8f6
Showing
19 changed files
with
705 additions
and
369 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
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 |
---|---|---|
@@ -1,29 +1,27 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
server/node_modules | ||
frontend/node_modules | ||
/server/node_modules | ||
/frontend/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
**/coverage/ | ||
|
||
# production | ||
frontend/build | ||
server/dist | ||
server/dist.zip | ||
/frontend/build | ||
/server/dist | ||
/server/dist.zip | ||
|
||
# misc | ||
.DS_Store | ||
.env | ||
.env.development | ||
.env.test | ||
.env.production | ||
# environment variables | ||
.env* | ||
cypress.env.json | ||
.secrets | ||
|
||
# misc | ||
.DS_Store | ||
|
||
# logs | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
yarn-error.log* |
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
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
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
This file was deleted.
Oops, something went wrong.
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,74 @@ | ||
import { configureStore, Store } from '@reduxjs/toolkit'; | ||
import pollReducer, { | ||
fetchPolls, | ||
addNewPoll, | ||
PollsState, | ||
initialState, | ||
} from '../features/pollSlice'; | ||
import * as api from '../server/api'; | ||
import { ThunkDispatch } from 'redux-thunk'; | ||
import { RootState } from '../store/store'; | ||
import { Action } from 'redux'; | ||
|
||
jest.mock('../server/api'); | ||
|
||
type AppThunkDispatch = ThunkDispatch<RootState, unknown, Action>; | ||
|
||
const mockStore = (state: PollsState) => | ||
configureStore({ | ||
reducer: { | ||
poll: pollReducer, | ||
}, | ||
preloadedState: { | ||
poll: state, | ||
}, | ||
}); | ||
|
||
describe('pollSlice', () => { | ||
let store: Store<RootState, Action> & { dispatch: AppThunkDispatch }; | ||
|
||
beforeEach(() => { | ||
store = mockStore(initialState) as Store<RootState, Action> & { | ||
dispatch: AppThunkDispatch; | ||
}; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should handle initial state', () => { | ||
expect(store.getState().poll).toEqual(initialState); | ||
}); | ||
|
||
it('should handle fetchPolls', async () => { | ||
const polls = [ | ||
{ id: '1', optionOne: 'Option 1', optionTwo: 'Option 2', votes: [] }, | ||
]; | ||
(api.fetchPolls as jest.Mock).mockResolvedValue({ polls }); | ||
|
||
await store.dispatch(fetchPolls()); | ||
|
||
const state = store.getState().poll; | ||
expect(state.polls['1']).toEqual(polls[0]); | ||
expect(state.status).toEqual('succeeded'); | ||
expect(state.error).toBeUndefined(); | ||
}); | ||
|
||
it('should handle addNewPoll', async () => { | ||
const newPoll = { | ||
id: '2', | ||
optionOne: 'Option 1', | ||
optionTwo: 'Option 2', | ||
userId: '1', | ||
votes: [], | ||
}; | ||
(api.createPoll as jest.Mock).mockResolvedValue(newPoll); | ||
|
||
await store.dispatch(addNewPoll(newPoll)); | ||
|
||
const state = store.getState().poll; | ||
expect(state.polls['2']).toEqual(newPoll); | ||
expect(state.status).toEqual('succeeded'); | ||
expect(state.error).toBeUndefined(); | ||
}); | ||
|
||
// Add more tests for voteOnPoll | ||
}); |
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,48 @@ | ||
import { render, fireEvent, screen, waitFor } from '@testing-library/react'; | ||
import CreatePollForm from '../components/CreatePollForm'; | ||
import { Provider } from 'react-redux'; | ||
import configureStore from 'redux-mock-store'; | ||
import { BrowserRouter as Router } from 'react-router-dom'; | ||
|
||
const mockStore = configureStore(); | ||
|
||
describe('CreatePollForm', () => { | ||
let store: ReturnType<typeof mockStore>; | ||
|
||
beforeEach(() => { | ||
store = mockStore({ | ||
users: { | ||
currentUser: { | ||
id: '123', | ||
name: 'User', | ||
username: 'User', | ||
pollCount: 1, | ||
voteCount: 0, | ||
}, | ||
users: [], | ||
status: 'succeeded', | ||
error: undefined, | ||
}, | ||
poll: { | ||
polls: {}, | ||
status: 'idle', | ||
error: undefined, | ||
}, | ||
}); | ||
store.dispatch = jest.fn(); | ||
}); | ||
|
||
it('renders form inputs', async () => { | ||
render( | ||
<Provider store={store}> | ||
<Router> | ||
<CreatePollForm /> | ||
</Router> | ||
</Provider>, | ||
); | ||
|
||
expect(screen.getByLabelText(/Option One/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/Option Two/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/Create Poll/i)).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.