|
| 1 | +import { shallowMount, mount } from '@vue/test-utils'; |
| 2 | +import BundleDetail from '@shell/detail/fleet.cattle.io.bundle.vue'; |
| 3 | + |
| 4 | +describe('view: fleet.cattle.io.bundle', () => { |
| 5 | + const mockStore = { |
| 6 | + getters: { |
| 7 | + 'i18n/t': (text: string) => text, |
| 8 | + 'i18n/exists': jest.fn(), |
| 9 | + currentStore: () => 'management', |
| 10 | + 'management/schemaFor': jest.fn(), |
| 11 | + 'management/all': () => [], |
| 12 | + 'management/pathExistsInSchema': () => false, |
| 13 | + 'type-map/optionsFor': () => ({}), |
| 14 | + 'type-map/headersFor': () => [], |
| 15 | + 'cluster/schemaFor': jest.fn(), |
| 16 | + 'resource-fetch/refreshFlag': () => false, |
| 17 | + }, |
| 18 | + dispatch: jest.fn(), |
| 19 | + }; |
| 20 | + |
| 21 | + const mockValue = { |
| 22 | + id: 'fleet-default/test-bundle', |
| 23 | + type: 'fleet.cattle.io.bundle', |
| 24 | + metadata: { |
| 25 | + name: 'test-bundle', |
| 26 | + namespace: 'fleet-default', |
| 27 | + labels: {}, |
| 28 | + }, |
| 29 | + spec: { targets: [] }, |
| 30 | + status: { conditions: [] }, |
| 31 | + targetClusters: [], |
| 32 | + }; |
| 33 | + |
| 34 | + const mockValueWithResources = { |
| 35 | + id: 'fleet-default/test-bundle', |
| 36 | + type: 'fleet.cattle.io.bundle', |
| 37 | + metadata: { |
| 38 | + name: 'test-bundle', |
| 39 | + namespace: 'fleet-default', |
| 40 | + labels: {}, |
| 41 | + }, |
| 42 | + spec: { targets: [] }, |
| 43 | + status: { |
| 44 | + conditions: [ |
| 45 | + { |
| 46 | + type: 'Ready', |
| 47 | + status: 'True', |
| 48 | + } |
| 49 | + ] |
| 50 | + }, |
| 51 | + targetClusters: [ |
| 52 | + { |
| 53 | + id: 'fleet-default/cluster-1', |
| 54 | + nameDisplay: 'cluster-1', |
| 55 | + metadata: { labels: { 'management.cattle.io/cluster-name': 'c-m-abc123' } }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }; |
| 59 | + |
| 60 | + const mockBundleDeployments = [ |
| 61 | + { |
| 62 | + metadata: { |
| 63 | + labels: { |
| 64 | + 'fleet.cattle.io/bundle-namespace': 'fleet-default', |
| 65 | + 'fleet.cattle.io/bundle-name': 'test-bundle', |
| 66 | + 'fleet.cattle.io/cluster-namespace': 'fleet-default', |
| 67 | + 'fleet.cattle.io/cluster': 'cluster-1', |
| 68 | + }, |
| 69 | + }, |
| 70 | + status: { |
| 71 | + resources: [ |
| 72 | + { |
| 73 | + kind: 'Deployment', |
| 74 | + apiVersion: 'apps/v1', |
| 75 | + namespace: 'default', |
| 76 | + name: 'nginx', |
| 77 | + }, |
| 78 | + { |
| 79 | + kind: 'Service', |
| 80 | + apiVersion: 'v1', |
| 81 | + namespace: 'default', |
| 82 | + name: 'nginx-svc', |
| 83 | + }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + }, |
| 87 | + ]; |
| 88 | + |
| 89 | + const fullMountGlobal = { |
| 90 | + mocks: { |
| 91 | + $store: mockStore, |
| 92 | + $fetchState: { pending: false }, |
| 93 | + $route: { query: {}, hash: '' }, |
| 94 | + $router: { |
| 95 | + applyQuery: jest.fn(), |
| 96 | + replace: jest.fn(), |
| 97 | + currentRoute: { _value: { hash: '' } }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + stubs: { |
| 101 | + ResourceTable: true, |
| 102 | + teleport: true, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + const createWrapper = (props = {}) => { |
| 107 | + return shallowMount(BundleDetail, { |
| 108 | + props: { |
| 109 | + value: mockValue, |
| 110 | + ...props, |
| 111 | + }, |
| 112 | + global: { |
| 113 | + mocks: { |
| 114 | + $store: mockStore, |
| 115 | + $fetchState: { pending: false }, |
| 116 | + $route: { query: {} }, |
| 117 | + $router: { applyQuery: jest.fn() }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }); |
| 121 | + }; |
| 122 | + |
| 123 | + it('should render the bundle detail page', () => { |
| 124 | + const wrapper = createWrapper(); |
| 125 | + |
| 126 | + expect(wrapper.html()).toMatchSnapshot(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should render the bundle detail page with full mount', () => { |
| 130 | + const wrapper = mount(BundleDetail, { |
| 131 | + props: { value: mockValue }, |
| 132 | + global: fullMountGlobal, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(wrapper.html()).toMatchSnapshot(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should show Loading when fetch is pending', () => { |
| 139 | + const wrapper = shallowMount(BundleDetail, { |
| 140 | + props: { value: mockValue }, |
| 141 | + global: { |
| 142 | + mocks: { |
| 143 | + $store: mockStore, |
| 144 | + $fetchState: { pending: true }, |
| 145 | + $route: { query: {} }, |
| 146 | + $router: { applyQuery: jest.fn() }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }); |
| 150 | + |
| 151 | + expect(wrapper.html()).toMatchSnapshot(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should render resources when bundle deployments exist', async() => { |
| 155 | + const wrapper = mount(BundleDetail, { |
| 156 | + props: { value: mockValueWithResources }, |
| 157 | + global: fullMountGlobal, |
| 158 | + }); |
| 159 | + |
| 160 | + // Simulate fetch completion by setting allBundleDeployments |
| 161 | + (wrapper.vm as any).allBundleDeployments = mockBundleDeployments; |
| 162 | + await wrapper.vm.$nextTick(); |
| 163 | + |
| 164 | + expect(wrapper.html()).toMatchSnapshot(); |
| 165 | + |
| 166 | + // Verify computed bundleResources are populated |
| 167 | + expect((wrapper.vm as any).bundleResources).toHaveLength(2); |
| 168 | + expect((wrapper.vm as any).bundleResources[0].name).toBe('nginx'); |
| 169 | + expect((wrapper.vm as any).bundleResources[1].name).toBe('nginx-svc'); |
| 170 | + }); |
| 171 | +}); |
0 commit comments