|
| 1 | +import { mount, createLocalVue, config } from '@vue/test-utils' |
| 2 | +import LicenseCode from '@/components/LicenseCode.vue' |
| 3 | +import VueI18n from 'vue-i18n' |
| 4 | +import Vuex from 'vuex' |
| 5 | + |
| 6 | +describe('LicenseCode.vue', () => { |
| 7 | + let wrapper |
| 8 | + let state |
| 9 | + let store |
| 10 | + |
| 11 | + // Always creates a shallow instance of component |
| 12 | + beforeEach(() => { |
| 13 | + const localVue = createLocalVue() |
| 14 | + localVue.use(VueI18n) |
| 15 | + localVue.use(Vuex) |
| 16 | + state = { |
| 17 | + attributionDetails: { |
| 18 | + creatorName: '', |
| 19 | + creatorProfileUrl: 'www.author.com', |
| 20 | + workTitle: 'My work', |
| 21 | + workUrl: 'www.author.com/pic.jpg' |
| 22 | + } |
| 23 | + } |
| 24 | + store = new Vuex.Store({ |
| 25 | + state |
| 26 | + }) |
| 27 | + const messages = require('@/locales/en.json') |
| 28 | + const i18n = new VueI18n({ |
| 29 | + locale: 'en', |
| 30 | + fallbackLocale: 'en', |
| 31 | + messages: messages, |
| 32 | + silentTranslationWarn: true |
| 33 | + }) |
| 34 | + |
| 35 | + config.mocks.i18n = i18n |
| 36 | + |
| 37 | + config.mocks.$t = (key) => { |
| 38 | + return i18n.messages[key] |
| 39 | + } |
| 40 | + wrapper = mount(LicenseCode, { |
| 41 | + localVue, |
| 42 | + store, |
| 43 | + i18n |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + // Test for DOM elements which must be present |
| 48 | + it('Check if the p-tag with class license-text is present in the DOM', () => { |
| 49 | + expect(wrapper.contains('.license-text')).toBe(true) |
| 50 | + }) |
| 51 | + |
| 52 | + it('Check if the LicenseCode.vue component renders without any errors', () => { |
| 53 | + expect(wrapper.isVueInstance()).toBeTruthy() |
| 54 | + }) |
| 55 | + |
| 56 | + // Tests for computed props and methods |
| 57 | + it('Check the creatorSpan function returns else text correctly', () => { |
| 58 | + expect(wrapper.vm.creatorSpan).toBe('') |
| 59 | + }) |
| 60 | + it('Check if the byString function returns else text correctly', () => { |
| 61 | + expect(wrapper.vm.byString).toBe('') |
| 62 | + }) |
| 63 | + it('Check if the byString function returns the correct text', () => { |
| 64 | + state.attributionDetails.creatorName = 'J Doe' |
| 65 | + expect(wrapper.vm.byString).toBe('license-use.richtext.by') |
| 66 | + }) |
| 67 | + it('Check if the creatorSpan function returns the correct text', () => { |
| 68 | + state.attributionDetails.creatorName = 'J Doe' |
| 69 | + expect(wrapper.vm.creatorSpan).toBe('<span rel="cc:attributionName">J Doe</span>') |
| 70 | + }) |
| 71 | + it('Check if the creatorName function returns the correct text', () => { |
| 72 | + state.attributionDetails.creatorName = 'J Doe' |
| 73 | + expect(wrapper.vm.creatorName).toBe('J Doe') |
| 74 | + }) |
| 75 | + it('Check if the creatorProfileUrl function returns the correct text', () => { |
| 76 | + expect(wrapper.vm.creatorProfileUrl).toBe('www.author.com') |
| 77 | + }) |
| 78 | + it('Check if the workTitle function returns the correct text', () => { |
| 79 | + expect(wrapper.vm.workTitle).toBe('My work') |
| 80 | + }) |
| 81 | + it('Check if the workUrl function returns the correct text', () => { |
| 82 | + expect(wrapper.vm.workUrl).toBe('www.author.com/pic.jpg') |
| 83 | + }) |
| 84 | + it('Check if the isWeb function returns true if attribution type is web', () => { |
| 85 | + wrapper.setProps({ |
| 86 | + attributionType: 'web' |
| 87 | + }) |
| 88 | + expect(wrapper.vm.isWeb).toBe(true) |
| 89 | + }) |
| 90 | + it('Check if the isWeb function returns false if attribution type is print', () => { |
| 91 | + wrapper.setProps({ |
| 92 | + attributionType: 'print' |
| 93 | + }) |
| 94 | + expect(wrapper.vm.isWeb).toBe(false) |
| 95 | + }) |
| 96 | + |
| 97 | + // Snapshot tests |
| 98 | + it('has the expected UI', () => { |
| 99 | + expect(wrapper).toMatchSnapshot() |
| 100 | + }) |
| 101 | +}) |
0 commit comments