-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for notification container
- Loading branch information
1 parent
d339009
commit 352832a
Showing
5 changed files
with
127 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export const props = { message: 'some notification' } | ||
|
||
export const storeStateMock = { | ||
boards: { | ||
isLoading: false, | ||
error: null, | ||
}, | ||
lists: { | ||
'board-1': { | ||
isLoading: false, | ||
error: null, | ||
}, | ||
}, | ||
members: { | ||
isLoading: false, | ||
error: null, | ||
}, | ||
} | ||
|
||
export const storeWithBoardError = { | ||
boards: { | ||
isLoading: false, | ||
error: { | ||
status: 401, | ||
responseText: 'example response', | ||
}, | ||
}, | ||
} | ||
|
||
export const storeWithAnyError = { | ||
user: { | ||
error: { message: 'some error user message ' }, | ||
}, | ||
} |
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 React from 'react' | ||
import { shallow } from 'enzyme' | ||
import configureMockStore from 'redux-mock-store' | ||
import thunk from 'redux-thunk' | ||
|
||
import { | ||
props, | ||
storeStateMock, | ||
storeWithBoardError, | ||
storeWithAnyError, | ||
} from '../__mocks__/notification' | ||
|
||
// inspired by https://github.com/reactjs/redux/issues/1534#issuecomment-205061049 | ||
const mockStore = configureMockStore([thunk]) | ||
|
||
describe('Component/NotificationContainer', () => { | ||
let store | ||
beforeEach(() => { | ||
window.localStorage.removeItem.mockReset() | ||
store = mockStore(storeStateMock) | ||
}) | ||
|
||
afterAll(() => { | ||
jest.resetModules() | ||
}) | ||
|
||
test('should get data from the store and prepare props without throwing an error', () => { | ||
const NotificationContainer = require('../').default | ||
const wrapper = shallow(<NotificationContainer store={store} {...props} someOtherProp />) | ||
|
||
const expectedProps = { | ||
autoHideDuration: 10000, | ||
message: 'some notification', | ||
someOtherProp: true, | ||
} | ||
|
||
expect(wrapper.props()).toEqual(expect.objectContaining(expectedProps)) | ||
}) | ||
|
||
test('should show a different message when board.error met certain conditions', () => { | ||
store = mockStore(storeWithBoardError) | ||
const NotificationContainer = require('../').default | ||
const wrapper = shallow(<NotificationContainer store={store} {...props} />) | ||
|
||
const expectedProps = { | ||
autoHideDuration: 60000, | ||
message: | ||
'example response: Please reload the page and try it again. Optional: delete localStorage as well.', | ||
} | ||
|
||
expect(wrapper.props()).toEqual(expect.objectContaining(expectedProps)) | ||
}) | ||
|
||
test('should remove an item from localStorage when board.error met certain conditions', () => { | ||
store = mockStore(storeWithBoardError) | ||
const NotificationContainer = require('../').default | ||
shallow(<NotificationContainer store={store} {...props} />) | ||
expect(window.localStorage.removeItem).toHaveBeenCalledTimes(1) | ||
expect(window.localStorage.removeItem).toHaveBeenCalledWith('trello_token') | ||
}) | ||
|
||
test('should show a default error message when other errors then the 401-board.error are available', () => { | ||
store = mockStore(storeWithAnyError) | ||
const NotificationContainer = require('../').default | ||
const wrapper = shallow(<NotificationContainer store={store} {...props} />) | ||
|
||
const expectedProps = { | ||
autoHideDuration: 10000, | ||
message: 'An error occured. Please try it again later.', | ||
} | ||
|
||
expect(wrapper.props()).toEqual(expect.objectContaining(expectedProps)) | ||
}) | ||
}) |
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