|
| 1 | +import { mount, createLocalVue, config } from '@vue/test-utils' |
| 2 | +import HelpSection from '@/components/HelpSection.vue' |
| 3 | +import VueI18n from 'vue-i18n' |
| 4 | +import Buefy from 'buefy' |
| 5 | + |
| 6 | +describe('HelpSection.vue', () => { |
| 7 | + let wrapper |
| 8 | + |
| 9 | + // Always creates a shallow instance of component |
| 10 | + beforeEach(() => { |
| 11 | + const localVue = createLocalVue() |
| 12 | + localVue.use(VueI18n) |
| 13 | + localVue.use(Buefy) |
| 14 | + const messages = require('@/locales/en.json') |
| 15 | + const i18n = new VueI18n({ |
| 16 | + locale: 'en', |
| 17 | + fallbackLocale: 'en', |
| 18 | + messages: messages, |
| 19 | + silentTranslationWarn: true |
| 20 | + }) |
| 21 | + |
| 22 | + config.mocks.i18n = i18n |
| 23 | + |
| 24 | + config.mocks.i18n.$t = (key) => { |
| 25 | + return i18n.messages[key] |
| 26 | + } |
| 27 | + config.mocks.$ga = { |
| 28 | + event: (sentObj) => { |
| 29 | + return sentObj |
| 30 | + } |
| 31 | + } |
| 32 | + wrapper = mount(HelpSection, { |
| 33 | + localVue, |
| 34 | + i18n |
| 35 | + }) |
| 36 | + wrapper.setData({ |
| 37 | + sixLicensesImg: '<img src="../assets/license-openness-scale.png">', |
| 38 | + modals: { |
| 39 | + 1: { |
| 40 | + status: false, |
| 41 | + title: 'what-are-cc-licenses' |
| 42 | + }, |
| 43 | + 2: { |
| 44 | + status: false, |
| 45 | + title: 'how-licenses-work' |
| 46 | + }, |
| 47 | + 3: { |
| 48 | + status: false, |
| 49 | + title: 'what-icons-mean' |
| 50 | + }, |
| 51 | + 4: { |
| 52 | + status: false, |
| 53 | + title: 'considerations-before-licensing' |
| 54 | + }, |
| 55 | + 5: { |
| 56 | + status: false, |
| 57 | + title: 'how-formally-license' |
| 58 | + }, |
| 59 | + 6: { |
| 60 | + status: false, |
| 61 | + title: 'six-cc-licenses' |
| 62 | + }, |
| 63 | + 7: { |
| 64 | + status: false, |
| 65 | + title: 'how-licenses-communicated' |
| 66 | + }, |
| 67 | + 8: { |
| 68 | + status: false, |
| 69 | + title: 'what-free-culture-license' |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('Check if HelpSection.vue component renders without any errors', () => { |
| 76 | + expect(wrapper.isVueInstance()).toBeTruthy() |
| 77 | + }) |
| 78 | + |
| 79 | + // Test for DOM elements which must be present |
| 80 | + it('Check if the help section is displayed in the DOM', () => { |
| 81 | + expect(wrapper.contains('.help-section')).toBe(true) |
| 82 | + }) |
| 83 | + it('Check if the main ul tag with help links is present', () => { |
| 84 | + expect(wrapper.contains('.help-links')).toBe(true) |
| 85 | + }) |
| 86 | + it('Check if all the 8 li tags are present in the DOM', () => { |
| 87 | + expect(wrapper.findAll('.help-link').length).toBe(8) |
| 88 | + }) |
| 89 | + it('Check if "What Are Creative Commons Licenses?" modal is rendered to DOM', () => { |
| 90 | + expect(wrapper.contains('ul > li:nth-child(1) > a')).toBe(true) |
| 91 | + wrapper.find('ul > li:nth-child(1) > a').trigger('click') |
| 92 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 93 | + }) |
| 94 | + it('Check if "How do the Licenses Work?" modal is rendered to DOM', () => { |
| 95 | + expect(wrapper.contains('ul > li:nth-child(2) > a')).toBe(true) |
| 96 | + wrapper.find('ul > li:nth-child(2) > a').trigger('click') |
| 97 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 98 | + }) |
| 99 | + it('Check if "What do the Icons Mean?" modal is rendered to DOM', () => { |
| 100 | + expect(wrapper.contains('ul > li:nth-child(3) > a')).toBe(true) |
| 101 | + wrapper.find('ul > li:nth-child(3) > a').trigger('click') |
| 102 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 103 | + }) |
| 104 | + it('Check if "Considerations Before Licensing" modal is rendered to DOM', () => { |
| 105 | + expect(wrapper.contains('ul > li:nth-child(4) > a')).toBe(true) |
| 106 | + wrapper.find('ul > li:nth-child(4) > a').trigger('click') |
| 107 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 108 | + }) |
| 109 | + it('Check if "How do I Formally License my Work?" modal is rendered to DOM', () => { |
| 110 | + expect(wrapper.contains('ul > li:nth-child(5) > a')).toBe(true) |
| 111 | + wrapper.find('ul > li:nth-child(5) > a').trigger('click') |
| 112 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 113 | + }) |
| 114 | + it('Check if "The Six Creative Commons Licenses" modal is rendered to DOM', () => { |
| 115 | + expect(wrapper.contains('ul > li:nth-child(6) > a')).toBe(true) |
| 116 | + wrapper.find('ul > li:nth-child(6) > a').trigger('click') |
| 117 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 118 | + }) |
| 119 | + it('Check if "How are Licenses Communicated?" modal is rendered to DOM', () => { |
| 120 | + expect(wrapper.contains('ul > li:nth-child(7) > a')).toBe(true) |
| 121 | + wrapper.find('ul > li:nth-child(7) > a').trigger('click') |
| 122 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 123 | + }) |
| 124 | + it('Check if " What is a Free Culture License?" modal is rendered to DOM', () => { |
| 125 | + expect(wrapper.contains('ul > li:nth-child(8) > a')).toBe(true) |
| 126 | + wrapper.find('ul > li:nth-child(8) > a').trigger('click') |
| 127 | + expect(wrapper.contains('.modal-content')).toBe(true) |
| 128 | + }) |
| 129 | + |
| 130 | + // Tests for methods |
| 131 | + it('Check if the data has the sixLicensesImg', () => { |
| 132 | + expect(wrapper.vm.sixLicensesImg).toBe('<img src="../assets/license-openness-scale.png">') |
| 133 | + }) |
| 134 | + it('Check if the is clickHandler working correctly', () => { |
| 135 | + wrapper.vm.clickHandler(1) |
| 136 | + expect(wrapper.vm.modals[1].status).toBe(true) |
| 137 | + }) |
| 138 | + it('Check if the $ga event is working in production', () => { |
| 139 | + process.env.NODE_ENV = 'production' |
| 140 | + expect(wrapper.vm.clickHandler(2)).toStrictEqual({ eventAction: 'clicked', eventCategory: 'HelpSection', eventLabel: 'how-licenses-work' }) |
| 141 | + expect(wrapper.vm.modals[2].status).toBe(true) |
| 142 | + expect(wrapper.vm.modals[1].status).toBe(false) |
| 143 | + }) |
| 144 | + |
| 145 | + // Snapshot tests |
| 146 | + it('Check if the HelpSection.vue component has the expected UI', () => { |
| 147 | + expect(wrapper).toMatchSnapshot() |
| 148 | + }) |
| 149 | +}) |
0 commit comments