|
| 1 | +/** |
| 2 | + * This source file is part of the Swift.org open source project |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | + * Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + * |
| 7 | + * See https://swift.org/LICENSE.txt for license information |
| 8 | + * See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | +import { shallowMount } from '@vue/test-utils'; |
| 11 | +import referencesProvider from 'docc-render/mixins/referencesProvider'; |
| 12 | + |
| 13 | +const FakeComponentInner = { |
| 14 | + name: 'FakeComponentInner', |
| 15 | + props: ['references'], |
| 16 | + render() { |
| 17 | + return null; |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +const FakeComponentOuter = { |
| 22 | + name: 'FakeComponentOuter', |
| 23 | + mixins: [referencesProvider], |
| 24 | + render(createElement) { |
| 25 | + return createElement(FakeComponentInner, { |
| 26 | + props: { |
| 27 | + references: this.references, |
| 28 | + }, |
| 29 | + }); |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +const aa = { |
| 34 | + identifier: 'doc://A/documentation/A/a', |
| 35 | + url: '/documentation/A/a', |
| 36 | + title: 'A.A', |
| 37 | +}; |
| 38 | +const ab = { |
| 39 | + identifier: 'doc://A/documentation/A/b', |
| 40 | + url: '/documentation/A/b', |
| 41 | + title: 'A.B', |
| 42 | +}; |
| 43 | +const bb = { |
| 44 | + identifier: 'doc://B/documentation/B/b', |
| 45 | + url: '/documentation/B/b', |
| 46 | + title: 'B.B', |
| 47 | +}; |
| 48 | +const bbb = { |
| 49 | + identifier: 'doc://BB/documentation/BB/b', |
| 50 | + url: '/documentation/BB/b', |
| 51 | + title: 'BB.B', |
| 52 | +}; |
| 53 | + |
| 54 | +const references = { |
| 55 | + [aa.identifier]: aa, |
| 56 | + [ab.identifier]: ab, |
| 57 | + [bb.identifier]: bb, |
| 58 | + [bbb.identifier]: bbb, |
| 59 | +}; |
| 60 | + |
| 61 | +const provide = { |
| 62 | + store: { |
| 63 | + state: { references }, |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +const createOuter = (opts = { provide }) => shallowMount(FakeComponentOuter, opts); |
| 68 | + |
| 69 | +describe('referencesProvider', () => { |
| 70 | + it('provides a store with a default state', () => { |
| 71 | + const outer = createOuter({}); |
| 72 | + const inner = outer.find(FakeComponentInner); |
| 73 | + expect(inner.exists()).toBe(true); |
| 74 | + expect(inner.props('references')).toEqual({}); |
| 75 | + }); |
| 76 | + |
| 77 | + it('provides references from a store', () => { |
| 78 | + const outer = createOuter(); |
| 79 | + const inner = outer.find(FakeComponentInner); |
| 80 | + expect(inner.exists()).toBe(true); |
| 81 | + expect(inner.props('references')).toEqual(references); |
| 82 | + }); |
| 83 | + |
| 84 | + it('removes `url` data for refs with non-empty `includedArchiveIdentifiers` app state', () => { |
| 85 | + // empty `includedArchiveIdentifiers` — no changes to refs |
| 86 | + const outer = createOuter(); |
| 87 | + let inner = outer.find(FakeComponentInner); |
| 88 | + expect(inner.exists()).toBe(true); |
| 89 | + expect(inner.props('references')).toEqual(references); |
| 90 | + |
| 91 | + // `includedArchiveIdentifiers` contains all refs - no changes to refs |
| 92 | + outer.setData({ |
| 93 | + appState: { |
| 94 | + includedArchiveIdentifiers: ['A', 'B', 'BB'], |
| 95 | + }, |
| 96 | + }); |
| 97 | + inner = outer.find(FakeComponentInner); |
| 98 | + expect(inner.exists()).toBe(true); |
| 99 | + expect(inner.props('references')).toEqual(references); |
| 100 | + |
| 101 | + // `includedArchiveIdentifiers` only contains archive B — remove `url` field |
| 102 | + // from all non-B refs |
| 103 | + outer.setData({ |
| 104 | + appState: { |
| 105 | + includedArchiveIdentifiers: ['B'], |
| 106 | + }, |
| 107 | + }); |
| 108 | + inner = outer.find(FakeComponentInner); |
| 109 | + expect(inner.exists()).toBe(true); |
| 110 | + const refs3 = inner.props('references'); |
| 111 | + expect(refs3).not.toEqual(references); |
| 112 | + expect(refs3[aa.identifier].title).toBe(aa.title); |
| 113 | + expect(refs3[aa.identifier].url).toBeFalsy(); // aa `url` is gone now |
| 114 | + expect(refs3[ab.identifier].title).toBe(ab.title); |
| 115 | + expect(refs3[ab.identifier].url).toBeFalsy(); // ab `url` is gone now |
| 116 | + expect(refs3[bb.identifier].title).toBe(bb.title); |
| 117 | + expect(refs3[bb.identifier].url).toBe(bb.url); // bb still has `url` |
| 118 | + expect(refs3[bbb.identifier].title).toBe(bbb.title); |
| 119 | + expect(refs3[bbb.identifier].url).toBeFalsy(); // bbb `url` is gone now |
| 120 | + }); |
| 121 | +}); |
0 commit comments