|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | +import { createStore, combineReducers, applyMiddleware } from 'redux'; |
| 4 | +import { Provider } from 'react-redux'; |
| 5 | +import thunk from 'redux-thunk'; |
| 6 | +import { AlertList, reducer } from '../index'; |
| 7 | + |
| 8 | +function setup(state = {}) { |
| 9 | + const store = createStore( |
| 10 | + combineReducers({ alerts: reducer }), |
| 11 | + state, |
| 12 | + applyMiddleware(thunk) |
| 13 | + ); |
| 14 | + const app = mount( |
| 15 | + <Provider store={store}> |
| 16 | + <AlertList /> |
| 17 | + </Provider> |
| 18 | + ); |
| 19 | + return { |
| 20 | + app, |
| 21 | + store |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +describe('AlertList', () => { |
| 26 | + let originalTimeout; |
| 27 | + beforeEach(function() { |
| 28 | + originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; |
| 29 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(function() { |
| 33 | + jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; |
| 34 | + }); |
| 35 | + it('should display messages and hide all after delay', () => { |
| 36 | + const { app } = setup({ alerts: { lastKey: 1, items: [{ key: 0 }, { key: 1 }] } }); |
| 37 | + expect(app.find('.alert').length).toBe(2); |
| 38 | + return new Promise(resolve => { |
| 39 | + setTimeout(() => { |
| 40 | + resolve(); |
| 41 | + }, 5000) |
| 42 | + }).then(() => { |
| 43 | + expect(app.find('.alert').length).toBe(0); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should display messages and hide itself after clicked', () => { |
| 48 | + const { app } = setup({ |
| 49 | + alerts: { |
| 50 | + lastKey: 1, |
| 51 | + items: [{ key: 0, messageText: 'messageText1' }, { key: 1, messageText: 'messageText2' }] |
| 52 | + } |
| 53 | + }); |
| 54 | + app.find('.alert').at(1).find('button').at(0) |
| 55 | + .simulate('click'); |
| 56 | + expect(app.find('.alert').text()).not.toMatch(/messageText2/); |
| 57 | + expect(app.find('.alert').text()).toMatch(/messageText1/); |
| 58 | + }); |
| 59 | +}); |
0 commit comments