|
| 1 | +import { shallowMount, createLocalVue } from '@vue/test-utils'; |
| 2 | +import Vuex from 'vuex'; |
| 3 | +import { GlModal } from '@gitlab/ui'; |
| 4 | +import GlModalVuex from '~/vue_shared/components/gl_modal_vuex.vue'; |
| 5 | +import createState from '~/vuex_shared/modules/modal/state'; |
| 6 | + |
| 7 | +const localVue = createLocalVue(); |
| 8 | +localVue.use(Vuex); |
| 9 | + |
| 10 | +const TEST_SLOT = 'Lorem ipsum modal dolar sit.'; |
| 11 | +const TEST_MODAL_ID = 'my-modal-id'; |
| 12 | +const TEST_MODULE = 'myModal'; |
| 13 | + |
| 14 | +describe('GlModalVuex', () => { |
| 15 | + let wrapper; |
| 16 | + let state; |
| 17 | + let actions; |
| 18 | + |
| 19 | + const factory = (options = {}) => { |
| 20 | + const store = new Vuex.Store({ |
| 21 | + modules: { |
| 22 | + [TEST_MODULE]: { |
| 23 | + namespaced: true, |
| 24 | + state, |
| 25 | + actions, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + const propsData = { |
| 31 | + modalId: TEST_MODAL_ID, |
| 32 | + modalModule: TEST_MODULE, |
| 33 | + ...options.propsData, |
| 34 | + }; |
| 35 | + |
| 36 | + wrapper = shallowMount(localVue.extend(GlModalVuex), { |
| 37 | + ...options, |
| 38 | + localVue, |
| 39 | + store, |
| 40 | + propsData, |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + state = createState(); |
| 46 | + |
| 47 | + actions = { |
| 48 | + show: jasmine.createSpy('show'), |
| 49 | + hide: jasmine.createSpy('hide'), |
| 50 | + }; |
| 51 | + }); |
| 52 | + |
| 53 | + it('renders gl-modal', () => { |
| 54 | + factory({ |
| 55 | + slots: { |
| 56 | + default: `<div>${TEST_SLOT}</div>`, |
| 57 | + }, |
| 58 | + }); |
| 59 | + const glModal = wrapper.find(GlModal); |
| 60 | + |
| 61 | + expect(glModal.props('modalId')).toBe(TEST_MODAL_ID); |
| 62 | + expect(glModal.text()).toContain(TEST_SLOT); |
| 63 | + }); |
| 64 | + |
| 65 | + it('passes props through to gl-modal', () => { |
| 66 | + const title = 'Test Title'; |
| 67 | + const okVariant = 'success'; |
| 68 | + |
| 69 | + factory({ |
| 70 | + propsData: { |
| 71 | + title, |
| 72 | + okTitle: title, |
| 73 | + okVariant, |
| 74 | + }, |
| 75 | + }); |
| 76 | + const glModal = wrapper.find(GlModal); |
| 77 | + |
| 78 | + expect(glModal.attributes('title')).toEqual(title); |
| 79 | + expect(glModal.attributes('oktitle')).toEqual(title); |
| 80 | + expect(glModal.attributes('okvariant')).toEqual(okVariant); |
| 81 | + }); |
| 82 | + |
| 83 | + it('passes listeners through to gl-modal', () => { |
| 84 | + const ok = jasmine.createSpy('ok'); |
| 85 | + |
| 86 | + factory({ |
| 87 | + listeners: { ok }, |
| 88 | + }); |
| 89 | + |
| 90 | + const glModal = wrapper.find(GlModal); |
| 91 | + glModal.vm.$emit('ok'); |
| 92 | + |
| 93 | + expect(ok).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('calls vuex action on show', () => { |
| 97 | + expect(actions.show).not.toHaveBeenCalled(); |
| 98 | + |
| 99 | + factory(); |
| 100 | + |
| 101 | + const glModal = wrapper.find(GlModal); |
| 102 | + glModal.vm.$emit('shown'); |
| 103 | + |
| 104 | + expect(actions.show).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | + |
| 107 | + it('calls vuex action on hide', () => { |
| 108 | + expect(actions.hide).not.toHaveBeenCalled(); |
| 109 | + |
| 110 | + factory(); |
| 111 | + |
| 112 | + const glModal = wrapper.find(GlModal); |
| 113 | + glModal.vm.$emit('hidden'); |
| 114 | + |
| 115 | + expect(actions.hide).toHaveBeenCalledTimes(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('calls bootstrap show when isVisible changes', done => { |
| 119 | + state.isVisible = false; |
| 120 | + |
| 121 | + factory(); |
| 122 | + const rootEmit = spyOn(wrapper.vm.$root, '$emit'); |
| 123 | + |
| 124 | + state.isVisible = true; |
| 125 | + |
| 126 | + localVue |
| 127 | + .nextTick() |
| 128 | + .then(() => { |
| 129 | + expect(rootEmit).toHaveBeenCalledWith('bv::show::modal', TEST_MODAL_ID); |
| 130 | + }) |
| 131 | + .then(done) |
| 132 | + .catch(done.fail); |
| 133 | + }); |
| 134 | + |
| 135 | + it('calls bootstrap hide when isVisible changes', done => { |
| 136 | + state.isVisible = true; |
| 137 | + |
| 138 | + factory(); |
| 139 | + const rootEmit = spyOn(wrapper.vm.$root, '$emit'); |
| 140 | + |
| 141 | + state.isVisible = false; |
| 142 | + |
| 143 | + localVue |
| 144 | + .nextTick() |
| 145 | + .then(() => { |
| 146 | + expect(rootEmit).toHaveBeenCalledWith('bv::hide::modal', TEST_MODAL_ID); |
| 147 | + }) |
| 148 | + .then(done) |
| 149 | + .catch(done.fail); |
| 150 | + }); |
| 151 | +}); |
0 commit comments