Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-cox committed Apr 22, 2024
1 parent a6f1f8d commit f3ec849
Showing 1 changed file with 234 additions and 0 deletions.
234 changes: 234 additions & 0 deletions shell/store/__tests__/catalog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import { CATALOG } from '@shell/config/types';
import { state, getters, actions, mutations } from '../catalog';
import Vuex from 'vuex';
import { createLocalVue } from '@vue/test-utils';
import Chart from '@shell/models/chart';

Check warning on line 5 in shell/store/__tests__/catalog.test.ts

View workflow job for this annotation

GitHub Actions / lint

'Chart' is defined but never used

type getterFnsType = keyof typeof getters

Check warning on line 7 in shell/store/__tests__/catalog.test.ts

View workflow job for this annotation

GitHub Actions / lint

'getterFnsType' is defined but never used
type MutationFnsType = keyof typeof mutations

Check warning on line 8 in shell/store/__tests__/catalog.test.ts

View workflow job for this annotation

GitHub Actions / lint

'MutationFnsType' is defined but never used
const mutationFns = Object.keys(mutations);

Check warning on line 9 in shell/store/__tests__/catalog.test.ts

View workflow job for this annotation

GitHub Actions / lint

'mutationFns' is assigned a value but never used

const convert = (namespace: string, obj: any) => {

Check warning on line 11 in shell/store/__tests__/catalog.test.ts

View workflow job for this annotation

GitHub Actions / lint

'convert' is assigned a value but never used
return Object.entries(obj).reduce((res, [key, v]) => {
res[`${ namespace }/${ key }`] = v;

return res;
}, {} as any);
};

const clusterRepo = { _key: 'testClusterRepo' };
const repoChartName = 'abc';
const repoChart = {
name: repoChartName,
type: 'namespaced',
version: 1,
metadata: { name: repoChartName }
};
const repoCharts = [repoChart];
const repo = {
metadata: { name: 'testRepo' },
_key: 'testRepo',
links: { index: 'fetchindex' },
canLoad: true,
followLink: () => ({ entries: { [repoChartName]: repoCharts } })
};

const catalogStoreName = 'catalog';
const clusterStore = 'cluster';

const expectedChartKey = `namespace/${ repo._key }/${ repoChartName }`;
const initialVersionInfo = { junk: true };
const initialRawChartName = 'cde';
const initialRawChart = {
id: `namespace/${ repo._key }/${ initialRawChartName }`,
repoKey: repo._key,
type: 'namespaced',
name: initialRawChartName,
version: 1,
metadata: { name: initialRawChartName }
};
const initialRawCharts = { [initialRawChart.id]: initialRawChart };
const initialLoadedInfo = { [repo._key]: true };

describe('catalog', () => {
describe('load', () => {
const constructStore = () => {
const catalogStore = {
state: state(),
getters,
mutations,
actions,
};

const localVue = createLocalVue();

localVue.use(Vuex);

return {
modules: {
i18n: {
namespaced: true,
getters: { withFallback: (state: any) => () => '' }
},
[catalogStoreName]: {
namespaced: true,
state: {
...catalogStore.state,
// Set initial state
// - repos have been loaded
// - some charts exist
// - some chart version info has been fetched
loaded: initialLoadedInfo,
charts: initialRawCharts,
versionInfos: initialVersionInfo,
},
mutations: catalogStore.mutations,
actions: catalogStore.actions,
getters: catalogStore.getters
},
[clusterStore]: {
namespaced: true,
getters: { schemaFor: (state) => (schema: string) => ({}) },
actions: {
findAll: (ctx, ...args: any[]) => {
if (args[0].type === CATALOG.CLUSTER_REPO) {
return Promise.resolve([clusterRepo]);
}
if (args[0].type === CATALOG.REPO) {
return Promise.resolve([repo]);
}

return Promise.resolve([]);
},
}
}
},
state: { $plugin: { getDynamic: () => require(`@shell/models/chart`) } },
getters: {
currentCluster: () => ({ }),
currentProduct: () => ({ inStore: clusterStore }),
},
mutations: { },
actions: { }
};
};

it('no force', async() => {
const localVue = createLocalVue();

localVue.use(Vuex);

const store = new Vuex.Store(constructStore());

// Validate initial state of store
expect(store.getters[`${ catalogStoreName }/rawCharts`]).toStrictEqual(initialRawCharts);
expect(store.getters[`${ catalogStoreName }/charts`]).toStrictEqual([]);

// Make the request
await store.dispatch(`${ catalogStoreName }/load`, {
force: false,
reset: false
});

// Basics should always be ok
expect(store.getters[`${ catalogStoreName }/inStore`]).toStrictEqual(clusterStore);
expect(store.getters[`${ catalogStoreName }/errors`]).toStrictEqual([]);
expect(store.getters[`${ catalogStoreName }/repos`]).toStrictEqual([clusterRepo, repo]);
expect(store.state[catalogStoreName].loaded).toStrictEqual(initialLoadedInfo);

// Store should still be in it's initial state (but charts populated)
expect(store.getters[`${ catalogStoreName }/rawCharts`]).toStrictEqual(initialRawCharts);
expect(store.getters[`${ catalogStoreName }/charts`]).toStrictEqual([initialRawChart]);

// We haven't reset the version info, so it should be unchanged
expect(store.state[catalogStoreName].versionInfos).toStrictEqual(initialVersionInfo);
});

it('force', async() => {
const localVue = createLocalVue();

localVue.use(Vuex);

const store = new Vuex.Store(constructStore());

// Validate initial state of store
let rawCharts = store.getters[`${ catalogStoreName }/rawCharts`];

expect(store.getters[`${ catalogStoreName }/rawCharts`]).toStrictEqual(initialRawCharts);
expect(rawCharts[initialRawChart.id]).toBeDefined();
expect(rawCharts[initialRawChart.id].id).toBe(initialRawChart.id);

expect(store.getters[`${ catalogStoreName }/charts`]).toStrictEqual([]);

// Make the request
await store.dispatch(`${ catalogStoreName }/load`, {
force: true, // Force such that we should get some values
reset: false
});

rawCharts = store.getters[`${ catalogStoreName }/rawCharts`];
const charts = store.getters[`${ catalogStoreName }/charts`];

// Basics should always be ok
expect(store.getters[`${ catalogStoreName }/inStore`]).toStrictEqual(clusterStore);
expect(store.getters[`${ catalogStoreName }/errors`]).toStrictEqual([]);
expect(store.getters[`${ catalogStoreName }/repos`]).toStrictEqual([clusterRepo, repo]);
expect(store.state[catalogStoreName].loaded).toStrictEqual(initialLoadedInfo);

// Store should have it's initial chart... and the new one
expect(rawCharts[initialRawChart.id]).toBeDefined();
expect(rawCharts[initialRawChart.id].id).toBe(initialRawChart.id);
expect(rawCharts[expectedChartKey]).toBeDefined();
expect(rawCharts[expectedChartKey].id).toBe(expectedChartKey);
expect(rawCharts[expectedChartKey].versions[0].version).toBe(repoChart.version);
expect(charts[0]).toBeDefined();
expect(charts[0].id).toBe(initialRawChart.id);
expect(charts[1]).toBeDefined();
expect(charts[1].id).toBe(expectedChartKey);

// Version info should be unchanged
expect(store.state[catalogStoreName].versionInfos).toStrictEqual(initialVersionInfo);
});

it('force + reset', async() => {
const localVue = createLocalVue();

localVue.use(Vuex);

const store = new Vuex.Store(constructStore());

// Validate initial state of store
expect(store.getters[`${ catalogStoreName }/rawCharts`]).toStrictEqual(initialRawCharts);
expect(store.getters[`${ catalogStoreName }/charts`]).toStrictEqual([]);

// Make the request
await store.dispatch(`${ catalogStoreName }/load`, {
force: true,
reset: true
});

const rawCharts = store.getters[`${ catalogStoreName }/rawCharts`];
const charts = store.getters[`${ catalogStoreName }/charts`];

// Basics should always be ok
expect(store.getters[`${ catalogStoreName }/inStore`]).toStrictEqual(clusterStore);
expect(store.getters[`${ catalogStoreName }/errors`]).toStrictEqual([]);
expect(store.getters[`${ catalogStoreName }/repos`]).toStrictEqual([clusterRepo, repo]);
expect(store.state[catalogStoreName].loaded).toStrictEqual(initialLoadedInfo);

// Store should NOT have it's initial chart... and should have the new one
expect(rawCharts[initialRawChart.id]).not.toBeDefined();
expect(rawCharts[expectedChartKey]).toBeDefined();
expect(rawCharts[expectedChartKey].id).toBe(expectedChartKey);
expect(rawCharts[expectedChartKey].versions[0].version).toBe(repoChart.version);

expect(charts).toHaveLength(1);
expect(charts[0]).toBeDefined();
expect(charts[0].id).toBe(expectedChartKey);
expect(charts[0].versions[0].version).toBe(repoChart.version);

// Version info should be changed (it's now empty given reset)
expect(store.state[catalogStoreName].versionInfos).toStrictEqual({ });
});
});
});

0 comments on commit f3ec849

Please sign in to comment.