Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {assert} from 'chai'; |
| 6 | import sinon from 'sinon'; |
| 7 | import * as ui from './ui.js'; |
| 8 | |
| 9 | |
| 10 | describe('ui', () => { |
| 11 | describe('reducers', () => { |
| 12 | describe('snackbarsReducer', () => { |
| 13 | it('adds snackbar', () => { |
| 14 | let state = ui.snackbarsReducer([], |
| 15 | {type: 'SHOW_SNACKBAR', id: 'one', text: 'A snackbar'}); |
| 16 | |
| 17 | assert.deepEqual(state, [{id: 'one', text: 'A snackbar'}]); |
| 18 | |
| 19 | state = ui.snackbarsReducer(state, |
| 20 | {type: 'SHOW_SNACKBAR', id: 'two', text: 'Another snack'}); |
| 21 | |
| 22 | assert.deepEqual(state, [ |
| 23 | {id: 'one', text: 'A snackbar'}, |
| 24 | {id: 'two', text: 'Another snack'}, |
| 25 | ]); |
| 26 | }); |
| 27 | |
| 28 | it('removes snackbar', () => { |
| 29 | let state = [ |
| 30 | {id: 'one', text: 'A snackbar'}, |
| 31 | {id: 'two', text: 'Another snack'}, |
| 32 | ]; |
| 33 | |
| 34 | state = ui.snackbarsReducer(state, |
| 35 | {type: 'HIDE_SNACKBAR', id: 'one'}); |
| 36 | |
| 37 | assert.deepEqual(state, [ |
| 38 | {id: 'two', text: 'Another snack'}, |
| 39 | ]); |
| 40 | |
| 41 | state = ui.snackbarsReducer(state, |
| 42 | {type: 'HIDE_SNACKBAR', id: 'two'}); |
| 43 | |
| 44 | assert.deepEqual(state, []); |
| 45 | }); |
| 46 | |
| 47 | it('does not remove non-existent snackbar', () => { |
| 48 | let state = [ |
| 49 | {id: 'one', text: 'A snackbar'}, |
| 50 | {id: 'two', text: 'Another snack'}, |
| 51 | ]; |
| 52 | |
| 53 | state = ui.snackbarsReducer(state, |
| 54 | {action: 'HIDE_SNACKBAR', id: 'whatever'}); |
| 55 | |
| 56 | assert.deepEqual(state, [ |
| 57 | {id: 'one', text: 'A snackbar'}, |
| 58 | {id: 'two', text: 'Another snack'}, |
| 59 | ]); |
| 60 | }); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe('selectors', () => { |
| 65 | it('snackbars', () => { |
| 66 | assert.deepEqual(ui.snackbars({ui: {snackbars: []}}), []); |
| 67 | assert.deepEqual(ui.snackbars({ui: {snackbars: [ |
| 68 | {text: 'Snackbar one', id: 'one'}, |
| 69 | {text: 'Snackbar two', id: 'two'}, |
| 70 | ]}}), [ |
| 71 | {text: 'Snackbar one', id: 'one'}, |
| 72 | {text: 'Snackbar two', id: 'two'}, |
| 73 | ]); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | describe('action creators', () => { |
| 78 | describe('showSnackbar', () => { |
| 79 | it('produces action', () => { |
| 80 | const action = ui.showSnackbar('id', 'text'); |
| 81 | const dispatch = sinon.stub(); |
| 82 | |
| 83 | action(dispatch); |
| 84 | |
| 85 | sinon.assert.calledWith(dispatch, |
| 86 | {type: 'SHOW_SNACKBAR', text: 'text', id: 'id'}); |
| 87 | }); |
| 88 | |
| 89 | it('hides snackbar after timeout', () => { |
| 90 | const clock = sinon.useFakeTimers(0); |
| 91 | |
| 92 | const action = ui.showSnackbar('id', 'text', 1000); |
| 93 | const dispatch = sinon.stub(); |
| 94 | |
| 95 | action(dispatch); |
| 96 | |
| 97 | sinon.assert.neverCalledWith(dispatch, |
| 98 | {type: 'HIDE_SNACKBAR', id: 'id'}); |
| 99 | |
| 100 | clock.tick(1000); |
| 101 | |
| 102 | sinon.assert.calledWith(dispatch, {type: 'HIDE_SNACKBAR', id: 'id'}); |
| 103 | |
| 104 | clock.restore(); |
| 105 | }); |
| 106 | |
| 107 | it('does not setTimeout when no timeout specified', () => { |
| 108 | sinon.stub(window, 'setTimeout'); |
| 109 | |
| 110 | ui.showSnackbar('id', 'text', 0); |
| 111 | |
| 112 | sinon.assert.notCalled(window.setTimeout); |
| 113 | |
| 114 | window.setTimeout.restore(); |
| 115 | }); |
| 116 | }); |
| 117 | |
| 118 | it('hideSnackbar produces action', () => { |
| 119 | assert.deepEqual(ui.hideSnackbar('one'), |
| 120 | {type: 'HIDE_SNACKBAR', id: 'one'}); |
| 121 | }); |
| 122 | }); |
| 123 | }); |