fix(editor): Move versions check to init function and refactor store (no-changelog) (#8067)

This commit is contained in:
Alex Grozav
2023-12-20 12:49:40 +02:00
committed by GitHub
parent faadfd6d4a
commit fcff34c401
10 changed files with 223 additions and 84 deletions

View File

@@ -3,14 +3,14 @@ import { useCloudPlanStore } from '@/stores/cloudPlan.store';
import { useSourceControlStore } from '@/stores/sourceControl.store';
import { useNodeTypesStore } from '@/stores/nodeTypes.store';
import { useRootStore } from '@/stores/n8nRoot.store';
import { initializeAuthenticatedFeatures } from '@/init';
import type { SpyInstance } from 'vitest';
import { initializeAuthenticatedFeatures, initializeCore } from '@/init';
import { createTestingPinia } from '@pinia/testing';
import { setActivePinia } from 'pinia';
import { useSettingsStore } from '@/stores/settings.store';
import { useVersionsStore } from '@/stores/versions.store';
vi.mock('@/stores/users.store', () => ({
useUsersStore: vi.fn(),
useUsersStore: vi.fn().mockReturnValue({ initialize: vi.fn() }),
}));
vi.mock('@/stores/n8nRoot.store', () => ({
@@ -18,22 +18,48 @@ vi.mock('@/stores/n8nRoot.store', () => ({
}));
describe('Init', () => {
describe('Authenticated Features', () => {
let settingsStore: ReturnType<typeof useSettingsStore>;
let cloudPlanStore: ReturnType<typeof useCloudPlanStore>;
let sourceControlStore: ReturnType<typeof useSourceControlStore>;
let nodeTypesStore: ReturnType<typeof useNodeTypesStore>;
let cloudStoreSpy: SpyInstance<[], Promise<void>>;
let templatesTestSpy: SpyInstance<[], Promise<void>>;
let sourceControlSpy: SpyInstance<[], Promise<void>>;
let nodeTranslationSpy: SpyInstance<[], Promise<void>>;
let settingsStore: ReturnType<typeof useSettingsStore>;
let cloudPlanStore: ReturnType<typeof useCloudPlanStore>;
let sourceControlStore: ReturnType<typeof useSourceControlStore>;
let usersStore: ReturnType<typeof useUsersStore>;
let nodeTypesStore: ReturnType<typeof useNodeTypesStore>;
let versionsStore: ReturnType<typeof useVersionsStore>;
beforeAll(() => {
setActivePinia(createTestingPinia());
settingsStore = useSettingsStore();
cloudPlanStore = useCloudPlanStore();
sourceControlStore = useSourceControlStore();
nodeTypesStore = useNodeTypesStore();
beforeEach(() => {
setActivePinia(createTestingPinia());
settingsStore = useSettingsStore();
cloudPlanStore = useCloudPlanStore();
sourceControlStore = useSourceControlStore();
nodeTypesStore = useNodeTypesStore();
usersStore = useUsersStore();
versionsStore = useVersionsStore();
versionsStore = useVersionsStore();
});
describe('initializeCore()', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('should initialize core features only once', async () => {
const usersStoreSpy = vi.spyOn(usersStore, 'initialize');
const settingsStoreSpy = vi.spyOn(settingsStore, 'initialize');
const versionsSpy = vi.spyOn(versionsStore, 'checkForNewVersions');
await initializeCore();
expect(settingsStoreSpy).toHaveBeenCalled();
expect(usersStoreSpy).toHaveBeenCalled();
expect(versionsSpy).toHaveBeenCalled();
await initializeCore();
expect(settingsStoreSpy).toHaveBeenCalledTimes(1);
});
});
describe('initializeAuthenticatedFeatures()', () => {
beforeEach(() => {
vi.spyOn(settingsStore, 'isCloudDeployment', 'get').mockReturnValue(true);
vi.spyOn(settingsStore, 'isTemplatesEnabled', 'get').mockReturnValue(true);
vi.spyOn(sourceControlStore, 'isEnterpriseSourceControlEnabled', 'get').mockReturnValue(true);
@@ -43,10 +69,6 @@ describe('Init', () => {
vi.mock('@/hooks/register', () => ({
initializeCloudHooks: vi.fn(),
}));
cloudStoreSpy = vi.spyOn(cloudPlanStore, 'initialize');
templatesTestSpy = vi.spyOn(settingsStore, 'testTemplatesEndpoint');
sourceControlSpy = vi.spyOn(sourceControlStore, 'getPreferences');
nodeTranslationSpy = vi.spyOn(nodeTypesStore, 'getNodeTranslationHeaders');
});
afterEach(() => {
@@ -54,24 +76,40 @@ describe('Init', () => {
});
it('should not init authenticated features if user is not logged in', async () => {
const cloudStoreSpy = vi.spyOn(cloudPlanStore, 'initialize');
const templatesTestSpy = vi.spyOn(settingsStore, 'testTemplatesEndpoint');
const sourceControlSpy = vi.spyOn(sourceControlStore, 'getPreferences');
const nodeTranslationSpy = vi.spyOn(nodeTypesStore, 'getNodeTranslationHeaders');
vi.mocked(useUsersStore).mockReturnValue({ currentUser: null } as ReturnType<
typeof useUsersStore
>);
await initializeAuthenticatedFeatures();
expect(cloudStoreSpy).not.toHaveBeenCalled();
expect(templatesTestSpy).not.toHaveBeenCalled();
expect(sourceControlSpy).not.toHaveBeenCalled();
expect(nodeTranslationSpy).not.toHaveBeenCalled();
});
it('should init authenticated features if user is not logged in', async () => {
it('should init authenticated features only once if user is logged in', async () => {
const cloudStoreSpy = vi.spyOn(cloudPlanStore, 'initialize');
const templatesTestSpy = vi.spyOn(settingsStore, 'testTemplatesEndpoint');
const sourceControlSpy = vi.spyOn(sourceControlStore, 'getPreferences');
const nodeTranslationSpy = vi.spyOn(nodeTypesStore, 'getNodeTranslationHeaders');
vi.mocked(useUsersStore).mockReturnValue({ currentUser: { id: '123' } } as ReturnType<
typeof useUsersStore
>);
await initializeAuthenticatedFeatures();
expect(cloudStoreSpy).toHaveBeenCalled();
expect(templatesTestSpy).toHaveBeenCalled();
expect(sourceControlSpy).toHaveBeenCalled();
expect(nodeTranslationSpy).toHaveBeenCalled();
await initializeAuthenticatedFeatures();
expect(cloudStoreSpy).toHaveBeenCalledTimes(1);
});
});
});