Skip to content

Commit

Permalink
chore(#158): update test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed May 12, 2022
1 parent 52c780b commit 374dde0
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
33 changes: 31 additions & 2 deletions packages/breadcrumb/test/components/DruxtBreadcrumb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ const mountComponent = ({ path, routes, propsData, options }) => {

// Add mock route to store.
for (const route of routes) {
let path = route
let label = route
let isHomePath = path === '/'
if (typeof route === 'object') {
path = route.path
label = route.label || path
isHomePath = route.isHomePath || undefined
}
store.commit('druxtRouter/addRoute', {
path: route,
path,
route: {
label: route
label,
isHomePath
}
})
}
Expand Down Expand Up @@ -59,6 +68,7 @@ describe('DruxtBreadcrumb', () => {
await wrapper.vm.$options.fetch.call(wrapper.vm)

expect(wrapper.vm.route).toStrictEqual({
isHomePath: true,
label: '/'
})
expect(Object.keys(wrapper.vm.routes)).toHaveLength(1)
Expand All @@ -79,6 +89,7 @@ describe('DruxtBreadcrumb', () => {
await wrapper.vm.$options.fetch.call(wrapper.vm)

expect(wrapper.vm.route).toStrictEqual({
isHomePath: false,
label: '/level-1'
})

Expand All @@ -96,6 +107,7 @@ describe('DruxtBreadcrumb', () => {
await wrapper.vm.$options.fetch.call(wrapper.vm)

expect(wrapper.vm.route).toStrictEqual({
isHomePath: false,
label: '/level-1/level-2'
})

Expand Down Expand Up @@ -128,6 +140,7 @@ describe('DruxtBreadcrumb', () => {
await wrapper.vm.$options.fetch.call(wrapper.vm)

expect(wrapper.vm.route).toStrictEqual({
isHomePath: false,
label: '/level-1/level-2'
})

Expand All @@ -150,11 +163,27 @@ describe('DruxtBreadcrumb', () => {
expect(wrapper.html()).toMatchSnapshot()
})

test('multilingual home', async () => {
const wrapper = mountComponent({ path: '/en/level-1', routes: ['/', {
label: 'Home',
path: '/en',
isHomePath: true
}] })
await wrapper.vm.$options.fetch.call(wrapper.vm)

expect(wrapper.vm.route).toStrictEqual({})
expect(wrapper.vm.crumbs).toStrictEqual([{
text: 'Home',
to: '/en'
}])
})

test('error', async () => {
const wrapper = mountComponent({ path: '/error/level-2', routes: ['/', '/error/level-2'] })
await wrapper.vm.$options.fetch.call(wrapper.vm)

expect(wrapper.vm.route).toStrictEqual({
isHomePath: false,
label: '/error/level-2'
})

Expand Down
17 changes: 17 additions & 0 deletions packages/druxt/test/components/DruxtModule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ describe('DruxtModule component', () => {
expect(wrapper.vm.$refs.module.value).toStrictEqual({ test: true })
})

test('watch - lang', async () => {
// Setup component.
const wrapper = mount(DruxtModule, { localVue, mocks })
wrapper.vm.$fetch = jest.fn()
await wrapper.vm.$options.fetch.call(wrapper.vm)

// Ensure defeaults.
expect(wrapper.vm.lang).toBe(undefined)

// Uppdate langcode.
await wrapper.setProps({ langcode: 'en' })

// Ensure fetch is called and computed prop is updated.
expect(wrapper.vm.$fetch).toHaveBeenCalled()
expect(wrapper.vm.lang).toBe('en')
})

test('error', async () => {
const CustomModule = {
name: 'CustomModule',
Expand Down
13 changes: 12 additions & 1 deletion packages/menu/test/components/DruxtMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DruxtStore } from '../../../druxt/src'
import { DruxtMenu, DruxtMenuStore } from '../../src'
import DruxtMenuComponent from '../../src/components/DruxtMenu.vue'
import DruxtMenuItemComponent from '../../src/components/DruxtMenuItem.vue'
import { expect } from '@jest/globals'

jest.mock('axios')

Expand Down Expand Up @@ -55,6 +54,7 @@ describe('DruxtMenu', () => {
'DruxtMenuDefault'
])
expect(Object.keys(wrapper.vm.getScopedSlots())).toStrictEqual(['default'])

// TODO - Add mock test data, Umami Profile has none.
// expect(wrapper.vm.model).toBe(1)
// expect(wrapper.vm.getScopedSlots().default()).toBe(1)
Expand Down Expand Up @@ -111,4 +111,15 @@ describe('DruxtMenu', () => {
value: wrapper.vm.model,
})
})

test('getEntitysByFilter', async() => {
const mock = {
getEntitiesByFilter: jest.fn()
}
DruxtMenuComponent.methods.getMenuItems.call(mock)
expect(mock.getEntitiesByFilter).toHaveBeenLastCalledWith({
filter: expect.any(Function),
prefix: undefined
})
})
})
3 changes: 3 additions & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"vue-router": "^3.0.0"
}
}
27 changes: 24 additions & 3 deletions packages/router/test/components/DruxtRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'regenerator-runtime/runtime'
import axios from 'axios'
import mockAxios from 'jest-mock-axios'
import { mount, createLocalVue } from '@vue/test-utils'
import VueRouter from 'vue-router'
import Vuex from 'vuex'

// import { DruxtDebug } from '../../../druxt/src/components/DruxtDebug.vue'
Expand All @@ -13,6 +14,7 @@ jest.mock('axios')

// Setup local vue instance.
const localVue = createLocalVue()
localVue.use(VueRouter)
localVue.use(Vuex)

const stubs = ['DruxtDebug', 'DruxtEntity']
Expand All @@ -33,7 +35,6 @@ const mocks = {
},
},
$redirect: jest.fn(),
$route: {},
app: {
context: {
error: jest.fn()
Expand All @@ -42,10 +43,11 @@ const mocks = {
}

const mountComponent = (propsData, fullPath, options = {}) => {
const router = new VueRouter()
if (fullPath) {
mocks.$route = { fullPath }
router.push({ path: fullPath })
}
return mount(DruxtRouterComponent, { localVue, mocks, propsData, store, stubs, ...options })
return mount(DruxtRouterComponent, { localVue, mocks, propsData, router, store, stubs, ...options })
}

describe('DruxtRouterComponent', () => {
Expand Down Expand Up @@ -242,4 +244,23 @@ describe('DruxtRouterComponent', () => {
expect(wrapper.vm.path).toBe('/node/1')
expect(wrapper.vm.route.resolvedPath).toBe('/en/recipes/deep-mediterranean-quiche')
})

test('Watch - $route', async () => {
// Setup component.
const wrapper = mountComponent({}, '/')
wrapper.vm.$fetch = wrapper.vm.$options.fetch
await wrapper.vm.$options.fetch.call(wrapper.vm)

// Assert default state.
expect(mockAxios.get).toHaveBeenNthCalledWith(1, '/router/translate-path?path=/', expect.any(Object))

// Change to mock route.
wrapper.vm.$store.state.druxtRouter.routes['/test'] = { test: true }
wrapper.vm.$router.push('/test')

// Assert the route has been updated.
await localVue.nextTick()
expect(wrapper.vm.$store.state.druxtRouter.route).toStrictEqual({ test: true })
})

})
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11012,6 +11012,7 @@ __metadata:
druxt: ^0.18.0
url-parse: ^1.5.9
vue: ^2.6.14
vue-router: ^3.0.0
vuex: ^3.6.2
peerDependencies:
drupal-jsonapi-params: "*"
Expand Down Expand Up @@ -22671,6 +22672,13 @@ typescript@^4.4.3:
languageName: node
linkType: hard

"vue-router@npm:^3.0.0":
version: 3.5.3
resolution: "vue-router@npm:3.5.3"
checksum: 7b2cc0d41ff2a8ec3da45761f29e14d0998119f47e4887f54e17eb534f7b4823acb778302891b0aa3a10c62ae8ba999393d7891cc08d36f02ea87aa268e2a36c
languageName: node
linkType: hard

"vue-template-compiler@npm:2.6.14, vue-template-compiler@npm:^2.6.14":
version: 2.6.14
resolution: "vue-template-compiler@npm:2.6.14"
Expand Down

0 comments on commit 374dde0

Please sign in to comment.