|
| 1 | +import { mount, createLocalVue, config } from '@vue/test-utils' |
| 2 | +import LicenseDetailsCard from '@/components/LicenseDetailsCard.vue' |
| 3 | +import { licenseSlug } from '@/utils/license-utilities' |
| 4 | +import VueI18n from 'vue-i18n' |
| 5 | +import Vuex from 'vuex' |
| 6 | + |
| 7 | +describe('LicenseDetailsCard.vue', () => { |
| 8 | + let wrapper |
| 9 | + let getters |
| 10 | + let store |
| 11 | + |
| 12 | + // Always creates a shallow instance of component |
| 13 | + beforeEach(() => { |
| 14 | + const localVue = createLocalVue() |
| 15 | + localVue.use(VueI18n) |
| 16 | + localVue.use(Vuex) |
| 17 | + getters = { |
| 18 | + shortName: () => { |
| 19 | + return 'CC BY-SA 4.0' |
| 20 | + }, |
| 21 | + fullName: () => { |
| 22 | + return 'Attribution-ShareAlike 4.0 International' |
| 23 | + }, |
| 24 | + licenseUrl: () => (mode) => { |
| 25 | + return 'https://creativecommons.org/licenses/by-sa/4.0/?ref=ccchooser' |
| 26 | + }, |
| 27 | + iconsList: () => { |
| 28 | + return ['by', 'sa'] |
| 29 | + } |
| 30 | + } |
| 31 | + store = new Vuex.Store({ |
| 32 | + getters |
| 33 | + }) |
| 34 | + const messages = require('@/locales/en.json') |
| 35 | + const i18n = new VueI18n({ |
| 36 | + locale: 'en', |
| 37 | + fallbackLocale: 'en', |
| 38 | + messages: messages, |
| 39 | + silentTranslationWarn: true |
| 40 | + }) |
| 41 | + |
| 42 | + config.mocks.i18n = i18n |
| 43 | + |
| 44 | + config.mocks.i18n.$t = (key) => { |
| 45 | + return i18n.messages[key] |
| 46 | + } |
| 47 | + wrapper = mount(LicenseDetailsCard, { |
| 48 | + localVue, |
| 49 | + store, |
| 50 | + i18n |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('Check if LicenseDetailsCard.vue component renders without any errors', () => { |
| 55 | + expect(wrapper.isVueInstance()).toBeTruthy() |
| 56 | + }) |
| 57 | + |
| 58 | + // Test for DOM elements which must be present |
| 59 | + it('Check if the main div-tag with class selected-license-card is present in the DOM', () => { |
| 60 | + expect(wrapper.contains('.selected-license-card')).toBe(true) |
| 61 | + }) |
| 62 | + it('Check if the heading of the license details card is present in the DOM', () => { |
| 63 | + expect(wrapper.contains('h3')).toBe(true) |
| 64 | + }) |
| 65 | + it('Check if the heading with full license name is present in the DOM', () => { |
| 66 | + expect(wrapper.contains('h4')).toBe(true) |
| 67 | + }) |
| 68 | + it('Check if the license heading is a link to the license', () => { |
| 69 | + expect(wrapper.contains('.license-name')).toBe(true) |
| 70 | + }) |
| 71 | + it('Check if the license description is present in the DOM', () => { |
| 72 | + expect(wrapper.contains('.chooser-selected-description')).toBe(true) |
| 73 | + }) |
| 74 | + it('Check if the visual-info section is present in the DOM', () => { |
| 75 | + expect(wrapper.contains('.license-visual-info')).toBe(true) |
| 76 | + }) |
| 77 | + it('Check if the license list is present in the DOM', () => { |
| 78 | + expect(wrapper.contains('.license-list')).toBe(true) |
| 79 | + }) |
| 80 | + it('Check if the license list elements are present in the DOM', () => { |
| 81 | + expect(wrapper.contains('li')).toBe(true) |
| 82 | + }) |
| 83 | + it('Check if the license list elements have description about them', () => { |
| 84 | + expect(wrapper.contains('.readable-string')).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + // Tests for computed props and methods |
| 88 | + it('Check if the slug function returns the correct text', () => { |
| 89 | + expect(wrapper.vm.slug).toBe(licenseSlug('CC BY-SA 4.0')) |
| 90 | + }) |
| 91 | + it('Check if the licenseKey function returns the correct text', () => { |
| 92 | + expect(wrapper.vm.licenseKey).toBe(`license-details-card.full-description.${licenseSlug('CC BY-SA 4.0')}`) |
| 93 | + }) |
| 94 | + it('Check if the licenseDescription function returns the correct text', () => { |
| 95 | + expect(wrapper.vm.licenseDescription).toBe(`${licenseSlug('CC BY-SA 4.0')}-description`) |
| 96 | + }) |
| 97 | + |
| 98 | + // Snapshot tests |
| 99 | + it('Check if the LicenseDetailsCard.vue component has the expected UI', () => { |
| 100 | + expect(wrapper).toMatchSnapshot() |
| 101 | + }) |
| 102 | +}) |
0 commit comments