|
1 | 1 | /* eslint-disable no-console */
|
2 | 2 | import { createLocalVue, mount, RouterLinkStub } from '@vue/test-utils'
|
3 | 3 | import VueRouter from 'vue-router'
|
4 |
| -import VueLink from '../lib' |
| 4 | +import { VueLink, VueLinkAddSlash, VueLinkStripSlash } from '../lib' |
5 | 5 |
|
6 | 6 | const localVue = createLocalVue()
|
7 | 7 | localVue.use(VueRouter)
|
@@ -32,6 +32,30 @@ describe('VueLink', () => {
|
32 | 32 |
|
33 | 33 | expect(link.vm.$props.to).toBe('/test')
|
34 | 34 | })
|
| 35 | + it('adds slashes', () => { |
| 36 | + const wrapper = mount(VueLinkAddSlash, { |
| 37 | + localVue, |
| 38 | + attachToDocument: true, |
| 39 | + stubs: { |
| 40 | + RouterLink: RouterLinkStub |
| 41 | + }, |
| 42 | + context: { |
| 43 | + props: { |
| 44 | + to: '/test' |
| 45 | + } |
| 46 | + }, |
| 47 | + slots: { |
| 48 | + default: '<div>Hi</div>' |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + expect(wrapper.isVueInstance()).toBe(true) |
| 53 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 54 | + |
| 55 | + const link = wrapper.find(RouterLinkStub) |
| 56 | + |
| 57 | + expect(link.vm.$props.to).toBe('/test/') |
| 58 | + }) |
35 | 59 | })
|
36 | 60 | describe('external', () => {
|
37 | 61 | it('does trigger external on http link', () => {
|
@@ -211,3 +235,201 @@ describe('VueLink', () => {
|
211 | 235 | })
|
212 | 236 | })
|
213 | 237 | })
|
| 238 | + |
| 239 | +describe('VueLinkStripSlash', () => { |
| 240 | + it('strips slashes', () => { |
| 241 | + const wrapper = mount(VueLinkStripSlash, { |
| 242 | + localVue, |
| 243 | + attachToDocument: true, |
| 244 | + stubs: { |
| 245 | + RouterLink: RouterLinkStub |
| 246 | + }, |
| 247 | + context: { |
| 248 | + props: { |
| 249 | + to: '/test/' |
| 250 | + } |
| 251 | + }, |
| 252 | + slots: { |
| 253 | + default: '<div>Hi</div>' |
| 254 | + } |
| 255 | + }) |
| 256 | + |
| 257 | + expect(wrapper.isVueInstance()).toBe(true) |
| 258 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 259 | + |
| 260 | + const link = wrapper.find(RouterLinkStub) |
| 261 | + |
| 262 | + expect(link.vm.$props.to).toBe('/test') |
| 263 | + }) |
| 264 | + it('strips from link with hash', () => { |
| 265 | + const wrapper = mount(VueLinkStripSlash, { |
| 266 | + localVue, |
| 267 | + attachToDocument: true, |
| 268 | + stubs: { |
| 269 | + RouterLink: RouterLinkStub |
| 270 | + }, |
| 271 | + context: { |
| 272 | + props: { |
| 273 | + to: '/test/' |
| 274 | + } |
| 275 | + }, |
| 276 | + slots: { |
| 277 | + default: '<div>Hi</div>' |
| 278 | + } |
| 279 | + }) |
| 280 | + |
| 281 | + expect(wrapper.isVueInstance()).toBe(true) |
| 282 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 283 | + |
| 284 | + const link = wrapper.find(RouterLinkStub) |
| 285 | + |
| 286 | + expect(link.vm.$props.to).toBe('/test') |
| 287 | + }) |
| 288 | + it('does not strip anything if no slash present', () => { |
| 289 | + const wrapper = mount(VueLinkStripSlash, { |
| 290 | + localVue, |
| 291 | + attachToDocument: true, |
| 292 | + stubs: { |
| 293 | + RouterLink: RouterLinkStub |
| 294 | + }, |
| 295 | + context: { |
| 296 | + props: { |
| 297 | + to: '/test#abc' |
| 298 | + } |
| 299 | + }, |
| 300 | + slots: { |
| 301 | + default: '<div>Hi</div>' |
| 302 | + } |
| 303 | + }) |
| 304 | + |
| 305 | + expect(wrapper.isVueInstance()).toBe(true) |
| 306 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 307 | + |
| 308 | + const link = wrapper.find(RouterLinkStub) |
| 309 | + |
| 310 | + expect(link.vm.$props.to).toBe('/test#abc') |
| 311 | + }) |
| 312 | + it('does not strip anything from link with hash without slash', () => { |
| 313 | + const wrapper = mount(VueLinkStripSlash, { |
| 314 | + localVue, |
| 315 | + attachToDocument: true, |
| 316 | + stubs: { |
| 317 | + RouterLink: RouterLinkStub |
| 318 | + }, |
| 319 | + context: { |
| 320 | + props: { |
| 321 | + to: '/test#abc' |
| 322 | + } |
| 323 | + }, |
| 324 | + slots: { |
| 325 | + default: '<div>Hi</div>' |
| 326 | + } |
| 327 | + }) |
| 328 | + |
| 329 | + expect(wrapper.isVueInstance()).toBe(true) |
| 330 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 331 | + |
| 332 | + const link = wrapper.find(RouterLinkStub) |
| 333 | + |
| 334 | + expect(link.vm.$props.to).toBe('/test#abc') |
| 335 | + }) |
| 336 | +}) |
| 337 | + |
| 338 | +describe('VueLinkAddSlash', () => { |
| 339 | + it('adds slashes', () => { |
| 340 | + const wrapper = mount(VueLinkAddSlash, { |
| 341 | + localVue, |
| 342 | + attachToDocument: true, |
| 343 | + stubs: { |
| 344 | + RouterLink: RouterLinkStub |
| 345 | + }, |
| 346 | + context: { |
| 347 | + props: { |
| 348 | + to: '/test' |
| 349 | + } |
| 350 | + }, |
| 351 | + slots: { |
| 352 | + default: '<div>Hi</div>' |
| 353 | + } |
| 354 | + }) |
| 355 | + |
| 356 | + expect(wrapper.isVueInstance()).toBe(true) |
| 357 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 358 | + |
| 359 | + const link = wrapper.find(RouterLinkStub) |
| 360 | + |
| 361 | + expect(link.vm.$props.to).toBe('/test/') |
| 362 | + }) |
| 363 | + it('adds slashes before hash', () => { |
| 364 | + const wrapper = mount(VueLinkAddSlash, { |
| 365 | + localVue, |
| 366 | + attachToDocument: true, |
| 367 | + stubs: { |
| 368 | + RouterLink: RouterLinkStub |
| 369 | + }, |
| 370 | + context: { |
| 371 | + props: { |
| 372 | + to: '/test#abc' |
| 373 | + } |
| 374 | + }, |
| 375 | + slots: { |
| 376 | + default: '<div>Hi</div>' |
| 377 | + } |
| 378 | + }) |
| 379 | + |
| 380 | + expect(wrapper.isVueInstance()).toBe(true) |
| 381 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 382 | + |
| 383 | + const link = wrapper.find(RouterLinkStub) |
| 384 | + |
| 385 | + expect(link.vm.$props.to).toBe('/test/#abc') |
| 386 | + }) |
| 387 | + it('does not add second slash', () => { |
| 388 | + const wrapper = mount(VueLinkAddSlash, { |
| 389 | + localVue, |
| 390 | + attachToDocument: true, |
| 391 | + stubs: { |
| 392 | + RouterLink: RouterLinkStub |
| 393 | + }, |
| 394 | + context: { |
| 395 | + props: { |
| 396 | + to: '/test/' |
| 397 | + } |
| 398 | + }, |
| 399 | + slots: { |
| 400 | + default: '<div>Hi</div>' |
| 401 | + } |
| 402 | + }) |
| 403 | + |
| 404 | + expect(wrapper.isVueInstance()).toBe(true) |
| 405 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 406 | + |
| 407 | + const link = wrapper.find(RouterLinkStub) |
| 408 | + |
| 409 | + expect(link.vm.$props.to).toBe('/test/') |
| 410 | + }) |
| 411 | + it('adds second slash before hash', () => { |
| 412 | + const wrapper = mount(VueLinkAddSlash, { |
| 413 | + localVue, |
| 414 | + attachToDocument: true, |
| 415 | + stubs: { |
| 416 | + RouterLink: RouterLinkStub |
| 417 | + }, |
| 418 | + context: { |
| 419 | + props: { |
| 420 | + to: '/test/#abc' |
| 421 | + } |
| 422 | + }, |
| 423 | + slots: { |
| 424 | + default: '<div>Hi</div>' |
| 425 | + } |
| 426 | + }) |
| 427 | + |
| 428 | + expect(wrapper.isVueInstance()).toBe(true) |
| 429 | + expect(wrapper.contains(RouterLinkStub)).toBe(true) |
| 430 | + |
| 431 | + const link = wrapper.find(RouterLinkStub) |
| 432 | + |
| 433 | + expect(link.vm.$props.to).toBe('/test/#abc') |
| 434 | + }) |
| 435 | +}) |
0 commit comments