From: Eduardo San Martin Morote Date: Tue, 2 Jul 2019 20:02:32 +0000 (+0200) Subject: test(link): add basic tests for router-link X-Git-Tag: v4.0.0-alpha.0~313 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=82d22471b5d5be0e52ac634f516a5f515bfedeee;p=thirdparty%2Fvuejs%2Frouter.git test(link): add basic tests for router-link --- diff --git a/__tests__/router-link.spec.js b/__tests__/router-link.spec.js new file mode 100644 index 00000000..c974eae4 --- /dev/null +++ b/__tests__/router-link.spec.js @@ -0,0 +1,139 @@ +/** + * @jest-environment jsdom + */ +// @ts-check +// NOTE: these tests only run when using jest `yarn jest --watch` +require('./helper') +const expect = require('expect') +const { default: RouterLink } = require('../src/components/Link') +const { components, isMocha, HistoryMock } = require('./utils') +const { START_LOCATION_NORMALIZED } = require('../src/types') + +/** @typedef {import('../src/types').RouteLocationNormalized} RouteLocationNormalized */ +/** @typedef {import('../src/types').RouteRecord} RouteRecord */ +/** @typedef {import('../src/types').RouteLocation} RouteLocation */ +/** @typedef {import('../src/types').MatcherLocation} MatcherLocation */ +/** @typedef {import('../src/types').RouteQueryAndHash} RouteQueryAndHash */ + +/** @type {Record }>} */ +const locations = { + basic: { + string: '/home', + // toResolve: { path: '/home', fullPath: '/home', undefined, query: {}, hash: '' }, + normalized: { + fullPath: '/home', + path: '/home', + params: {}, + // meta: {}, + query: {}, + hash: '', + matched: [], + name: undefined, + }, + }, + withQuery: { + string: '/home?foo=a&bar=b', + // toResolve: { path: '/home', fullPath: '/home', undefined, query: {}, hash: '' }, + normalized: { + fullPath: '/home?foo=a&bar=b', + path: '/home', + params: {}, + // meta: {}, + query: { foo: 'a', bar: 'b' }, + hash: '', + matched: [], + name: undefined, + }, + }, +} + +describe('RouterLink', () => { + // skip these tests on mocha because @vue/test-utils + // do not work correctly + if (isMocha()) return + const { mount } = require('@vue/test-utils') + + /** + * + * @param {RouteLocationNormalized} currentLocation + * @param {Object} propsData + * @param {RouteLocationNormalized} resolvedLocation + */ + function factory(currentLocation, propsData, resolvedLocation) { + const router = { + history: new HistoryMock(), + resolveLocation: jest.fn(), + push: jest.fn(), + } + + router.resolveLocation.mockReturnValueOnce(resolvedLocation) + // @ts-ignore TODO: Some information are missing on RouterLink + const wrapper = mount(RouterLink, { + propsData, + slots: { + default: 'a link', + }, + // stubs: { RouterLink }, + mocks: { $route: currentLocation, $router: router }, + }) + return { wrapper, router } + } + + it('displays a link with a string prop', () => { + const { wrapper } = factory( + START_LOCATION_NORMALIZED, + { to: locations.basic.string }, + locations.basic.normalized + ) + expect(wrapper.html()).toMatchInlineSnapshot( + `"a link"` + ) + }) + + it('displays a link with an object with path prop', () => { + const { wrapper } = factory( + START_LOCATION_NORMALIZED, + { to: { path: locations.basic.string } }, + locations.basic.normalized + ) + expect(wrapper.html()).toMatchInlineSnapshot( + `"a link"` + ) + }) + + it('calls ensureLocation', () => { + const { router } = factory( + START_LOCATION_NORMALIZED, + { to: locations.basic.string }, + locations.basic.normalized + ) + expect(router.resolveLocation).toHaveBeenCalledTimes(1) + expect(router.resolveLocation).toHaveBeenCalledWith( + expect.objectContaining({ path: locations.basic.string }), + START_LOCATION_NORMALIZED + ) + }) + + it('calls router.push when clicked', () => { + const { router, wrapper } = factory( + START_LOCATION_NORMALIZED, + { to: locations.basic.string }, + locations.basic.normalized + ) + wrapper.trigger('click', {}) + expect(router.push).toHaveBeenCalledTimes(1) + expect(router.push).toHaveBeenCalledWith(locations.basic.normalized) + }) + + it('normalizes query with path', () => { + const { router } = factory( + START_LOCATION_NORMALIZED, + { to: locations.withQuery.string }, + locations.withQuery.normalized // it doesn't matter as we want to check what resolveLocation is called with + ) + expect(router.resolveLocation).toHaveBeenCalledWith( + expect.objectContaining({ query: locations.withQuery.normalized.query }), + START_LOCATION_NORMALIZED + ) + }) +}) diff --git a/src/components/Link.ts b/src/components/Link.ts index 18462379..597bea8e 100644 --- a/src/components/Link.ts +++ b/src/components/Link.ts @@ -41,7 +41,7 @@ const Link: Component = { ...location, }) } - const route = router.resolveLocation(url, from) + const route = location // TODO: active classes // TODO: handle replace prop @@ -79,9 +79,8 @@ function guardEvent(e: MouseEvent) { if (/\b_blank\b/i.test(target)) return } // this may be a Weex event which doesn't have this method - if (e.preventDefault) { - e.preventDefault() - } + if (e.preventDefault) e.preventDefault() + return true }