|
1 | 1 | import { NavLink, NavSegment } from '../nav-util';
|
2 |
| -import { UrlSerializer, isPartMatch, fillMatchedUrlParts, parseUrlParts, createMatchedData, normalizeLinks } from '../url-serializer'; |
| 2 | +import { UrlSerializer, isPartMatch, fillMatchedUrlParts, parseUrlParts, createMatchedData, normalizeLinks, findLinkByComponentData } from '../url-serializer'; |
3 | 3 | import { mockDeepLinkConfig, noop, MockView1, MockView2, MockView3, MockView4, MockView5 } from '../../util/mock-providers';
|
4 | 4 |
|
5 | 5 |
|
6 | 6 | describe('UrlSerializer', () => {
|
7 | 7 |
|
8 | 8 | describe('serializeComponent', () => {
|
9 | 9 |
|
| 10 | + it('should create segement when config has multiple links to same component', () => { |
| 11 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 12 | + const link2 = { component: MockView1, name: 'viewtwo', segment: 'view/:param1' }; |
| 13 | + const link3 = { component: MockView1, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 14 | + |
| 15 | + serializer = mockSerializer([link1, link2, link3]); |
| 16 | + serializer.createSegment = noop; |
| 17 | + spyOn(serializer, 'createSegment'); |
| 18 | + serializer.serializeComponent(MockView1, null); |
| 19 | + expect(serializer.createSegment).toHaveBeenCalledWith(link1, null); |
| 20 | + }); |
| 21 | + |
10 | 22 | it('should create segment if component found in links', () => {
|
11 | 23 | serializer.createSegment = noop;
|
12 | 24 | spyOn(serializer, 'createSegment');
|
@@ -533,6 +545,88 @@ describe('UrlSerializer', () => {
|
533 | 545 |
|
534 | 546 | });
|
535 | 547 |
|
| 548 | + describe('findLinkByComponentData', () => { |
| 549 | + |
| 550 | + it('should get matching link by component w/ data and multiple links using same component, 2 matches', () => { |
| 551 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 552 | + const link2 = { component: MockView1, name: 'viewtwo', segment: 'view/:param1' }; |
| 553 | + const link3 = { component: MockView1, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 554 | + |
| 555 | + let links = normalizeLinks([link1, link2, link3]); |
| 556 | + |
| 557 | + let foundLink = findLinkByComponentData(links, MockView1, { |
| 558 | + param1: false, |
| 559 | + param2: 0, |
| 560 | + param3: 0 |
| 561 | + }); |
| 562 | + expect(foundLink.name).toEqual('viewthree'); |
| 563 | + }); |
| 564 | + |
| 565 | + it('should get matching link by component w/ data and multiple links using same component, 1 match', () => { |
| 566 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 567 | + const link2 = { component: MockView1, name: 'viewtwo', segment: 'view/:param1' }; |
| 568 | + const link3 = { component: MockView1, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 569 | + |
| 570 | + let links = normalizeLinks([link1, link2, link3]); |
| 571 | + |
| 572 | + let foundLink = findLinkByComponentData(links, MockView1, { |
| 573 | + param1: false, |
| 574 | + param3: 0 |
| 575 | + }); |
| 576 | + expect(foundLink.name).toEqual('viewtwo'); |
| 577 | + }); |
| 578 | + |
| 579 | + it('should get matching link by component w/ no data and multiple links using same component', () => { |
| 580 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 581 | + const link2 = { component: MockView1, name: 'viewtwo', segment: 'view/:param1' }; |
| 582 | + const link3 = { component: MockView1, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 583 | + |
| 584 | + let links = normalizeLinks([link1, link2, link3]); |
| 585 | + |
| 586 | + let foundLink = findLinkByComponentData(links, MockView1, null); |
| 587 | + expect(foundLink.name).toEqual('viewone'); |
| 588 | + }); |
| 589 | + |
| 590 | + it('should get matching link by component data and link data', () => { |
| 591 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 592 | + const link2 = { component: MockView2, name: 'viewtwo', segment: 'view/:param1' }; |
| 593 | + const link3 = { component: MockView3, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 594 | + |
| 595 | + let links = normalizeLinks([link1, link2, link3]); |
| 596 | + |
| 597 | + let foundLink = findLinkByComponentData(links, MockView3, { |
| 598 | + param1: null, |
| 599 | + param2: false, |
| 600 | + param3: 0, |
| 601 | + param4: 'hello' |
| 602 | + }); |
| 603 | + expect(foundLink.name).toEqual('viewthree'); |
| 604 | + }); |
| 605 | + |
| 606 | + it('should get matching link by component without data and link without data', () => { |
| 607 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 608 | + const link2 = { component: MockView2, name: 'viewtwo', segment: 'view/:param1' }; |
| 609 | + const link3 = { component: MockView3, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 610 | + |
| 611 | + let links = normalizeLinks([link1, link2, link3]); |
| 612 | + |
| 613 | + let foundLink = findLinkByComponentData(links, MockView1, null); |
| 614 | + expect(foundLink.name).toEqual('viewone'); |
| 615 | + }); |
| 616 | + |
| 617 | + it('should get no matching link by component without data, but link requires data', () => { |
| 618 | + const link1 = { component: MockView1, name: 'viewone', segment: 'view' }; |
| 619 | + const link2 = { component: MockView2, name: 'viewtwo', segment: 'view/:param1' }; |
| 620 | + const link3 = { component: MockView3, name: 'viewthree', segment: 'view/:param1/:param2' }; |
| 621 | + |
| 622 | + let links = normalizeLinks([link1, link2, link3]); |
| 623 | + |
| 624 | + let foundLink = findLinkByComponentData(links, MockView2, null); |
| 625 | + expect(foundLink).toEqual(null); |
| 626 | + }); |
| 627 | + |
| 628 | + }); |
| 629 | + |
536 | 630 | describe('normalizeLinks', () => {
|
537 | 631 |
|
538 | 632 | it('should sort with four parts, the most number of paths w/out data first', () => {
|
|
0 commit comments