debian-mirror-gitlab/spec/frontend/ide/components/preview/clientside_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

417 lines
12 KiB
JavaScript
Raw Normal View History

2019-12-04 20:38:33 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
2022-04-04 11:22:00 +05:30
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import { dispatch } from 'codesandbox-api';
2019-12-04 20:38:33 +05:30
import smooshpack from 'smooshpack';
2021-03-11 19:13:27 +05:30
import Vuex from 'vuex';
2022-04-04 11:22:00 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2019-12-04 20:38:33 +05:30
import Clientside from '~/ide/components/preview/clientside.vue';
2022-04-04 11:22:00 +05:30
import { PING_USAGE_PREVIEW_KEY, PING_USAGE_PREVIEW_SUCCESS_KEY } from '~/ide/constants';
2021-03-08 18:12:59 +05:30
import eventHub from '~/ide/eventhub';
2019-12-04 20:38:33 +05:30
jest.mock('smooshpack', () => ({
Manager: jest.fn(),
}));
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2019-12-04 20:38:33 +05:30
const dummyPackageJson = () => ({
raw: JSON.stringify({
main: 'index.js',
}),
});
2020-04-08 14:13:33 +05:30
const expectedSandpackOptions = () => ({
files: {},
entry: '/index.js',
showOpenInCodeSandbox: true,
});
const expectedSandpackSettings = () => ({
fileResolver: {
isFile: expect.any(Function),
readFile: expect.any(Function),
},
});
2019-12-04 20:38:33 +05:30
describe('IDE clientside preview', () => {
let wrapper;
let store;
const storeActions = {
getFileData: jest.fn().mockReturnValue(Promise.resolve({})),
getRawFileData: jest.fn().mockReturnValue(Promise.resolve('')),
};
2019-12-26 22:10:19 +05:30
const storeClientsideActions = {
pingUsage: jest.fn().mockReturnValue(Promise.resolve({})),
};
2022-04-04 11:22:00 +05:30
const dispatchCodesandboxReady = () => dispatch({ type: 'done' });
2019-12-04 20:38:33 +05:30
const createComponent = ({ state, getters } = {}) => {
store = new Vuex.Store({
state: {
entries: {},
links: {},
...state,
},
getters: {
packageJson: () => '',
currentProject: () => ({
visibility: 'public',
}),
...getters,
},
actions: storeActions,
2019-12-26 22:10:19 +05:30
modules: {
clientside: {
namespaced: true,
actions: storeClientsideActions,
},
},
2019-12-04 20:38:33 +05:30
});
wrapper = shallowMount(Clientside, {
store,
});
};
2021-03-08 18:12:59 +05:30
const createInitializedComponent = () => {
createComponent();
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2021-03-08 18:12:59 +05:30
wrapper.setData({
sandpackReady: true,
manager: {
listener: jest.fn(),
updatePreview: jest.fn(),
},
});
};
2019-12-04 20:38:33 +05:30
afterEach(() => {
wrapper.destroy();
});
describe('without main entry', () => {
it('creates sandpack manager', () => {
createComponent();
expect(smooshpack.Manager).not.toHaveBeenCalled();
});
});
describe('with main entry', () => {
beforeEach(() => {
createComponent({ getters: { packageJson: dummyPackageJson } });
2019-12-26 22:10:19 +05:30
2022-04-04 11:22:00 +05:30
return waitForPromises();
2019-12-04 20:38:33 +05:30
});
2020-04-08 14:13:33 +05:30
it('creates sandpack manager', () => {
expect(smooshpack.Manager).toHaveBeenCalledWith(
'#ide-preview',
expectedSandpackOptions(),
expectedSandpackSettings(),
);
});
it('pings usage', () => {
expect(storeClientsideActions.pingUsage).toHaveBeenCalledTimes(1);
2022-04-04 11:22:00 +05:30
expect(storeClientsideActions.pingUsage).toHaveBeenCalledWith(
expect.anything(),
PING_USAGE_PREVIEW_KEY,
);
});
it('pings usage success', async () => {
dispatchCodesandboxReady();
await nextTick();
expect(storeClientsideActions.pingUsage).toHaveBeenCalledTimes(2);
expect(storeClientsideActions.pingUsage).toHaveBeenCalledWith(
expect.anything(),
PING_USAGE_PREVIEW_SUCCESS_KEY,
);
2020-04-08 14:13:33 +05:30
});
});
describe('with codesandboxBundlerUrl', () => {
const TEST_BUNDLER_URL = 'https://test.gitlab-static.test';
beforeEach(() => {
createComponent({
getters: { packageJson: dummyPackageJson },
state: { codesandboxBundlerUrl: TEST_BUNDLER_URL },
});
2022-04-04 11:22:00 +05:30
return waitForPromises();
2020-04-08 14:13:33 +05:30
});
it('creates sandpack manager with bundlerURL', () => {
expect(smooshpack.Manager).toHaveBeenCalledWith('#ide-preview', expectedSandpackOptions(), {
...expectedSandpackSettings(),
bundlerURL: TEST_BUNDLER_URL,
});
});
});
describe('with codesandboxBundlerURL', () => {
beforeEach(() => {
createComponent({ getters: { packageJson: dummyPackageJson } });
2022-04-04 11:22:00 +05:30
return waitForPromises();
2020-04-08 14:13:33 +05:30
});
2019-12-04 20:38:33 +05:30
it('creates sandpack manager', () => {
expect(smooshpack.Manager).toHaveBeenCalledWith(
'#ide-preview',
{
files: {},
entry: '/index.js',
showOpenInCodeSandbox: true,
},
{
fileResolver: {
isFile: expect.any(Function),
readFile: expect.any(Function),
},
},
);
});
});
describe('computed', () => {
describe('normalizedEntries', () => {
it('returns flattened list of blobs with content', () => {
createComponent({
state: {
entries: {
'index.js': { type: 'blob', raw: 'test' },
'index2.js': { type: 'blob', content: 'content' },
tree: { type: 'tree' },
empty: { type: 'blob' },
},
},
});
expect(wrapper.vm.normalizedEntries).toEqual({
'/index.js': {
code: 'test',
},
'/index2.js': {
code: 'content',
},
});
});
});
describe('mainEntry', () => {
it('returns false when package.json is empty', () => {
createComponent();
expect(wrapper.vm.mainEntry).toBe(false);
});
it('returns main key from package.json', () => {
createComponent({ getters: { packageJson: dummyPackageJson } });
expect(wrapper.vm.mainEntry).toBe('index.js');
});
});
describe('showPreview', () => {
it('returns false if no mainEntry', () => {
createComponent();
expect(wrapper.vm.showPreview).toBe(false);
});
it('returns false if loading and mainEntry exists', () => {
createComponent({ getters: { packageJson: dummyPackageJson } });
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: true });
expect(wrapper.vm.showPreview).toBe(false);
});
it('returns true if not loading and mainEntry exists', () => {
createComponent({ getters: { packageJson: dummyPackageJson } });
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: false });
expect(wrapper.vm.showPreview).toBe(true);
});
});
describe('showEmptyState', () => {
it('returns true if no mainEntry exists', () => {
createComponent();
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: false });
expect(wrapper.vm.showEmptyState).toBe(true);
});
it('returns false if loading', () => {
createComponent();
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: true });
expect(wrapper.vm.showEmptyState).toBe(false);
});
it('returns false if not loading and mainEntry exists', () => {
createComponent({ getters: { packageJson: dummyPackageJson } });
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: false });
expect(wrapper.vm.showEmptyState).toBe(false);
});
});
describe('showOpenInCodeSandbox', () => {
2019-12-26 22:10:19 +05:30
it('returns true when visibility is public', () => {
2019-12-04 20:38:33 +05:30
createComponent({ getters: { currentProject: () => ({ visibility: 'public' }) } });
expect(wrapper.vm.showOpenInCodeSandbox).toBe(true);
});
2019-12-26 22:10:19 +05:30
it('returns false when visibility is private', () => {
2019-12-04 20:38:33 +05:30
createComponent({ getters: { currentProject: () => ({ visibility: 'private' }) } });
expect(wrapper.vm.showOpenInCodeSandbox).toBe(false);
});
});
describe('sandboxOpts', () => {
beforeEach(() => {
createComponent({
state: {
entries: {
'index.js': { type: 'blob', raw: 'test' },
'package.json': dummyPackageJson(),
},
},
getters: {
packageJson: dummyPackageJson,
},
});
});
it('returns sandbox options', () => {
expect(wrapper.vm.sandboxOpts).toEqual({
files: {
'/index.js': {
code: 'test',
},
'/package.json': {
code: '{"main":"index.js"}',
},
},
entry: '/index.js',
showOpenInCodeSandbox: true,
});
});
});
});
describe('methods', () => {
describe('loadFileContent', () => {
beforeEach(() => {
createComponent();
return wrapper.vm.loadFileContent('package.json');
});
it('calls getFileData', () => {
2020-11-24 15:15:51 +05:30
expect(storeActions.getFileData).toHaveBeenCalledWith(expect.any(Object), {
path: 'package.json',
makeFileActive: false,
});
2019-12-04 20:38:33 +05:30
});
it('calls getRawFileData', () => {
2020-11-24 15:15:51 +05:30
expect(storeActions.getRawFileData).toHaveBeenCalledWith(expect.any(Object), {
path: 'package.json',
});
2019-12-04 20:38:33 +05:30
});
});
describe('update', () => {
it('initializes manager if manager is empty', () => {
createComponent({ getters: { packageJson: dummyPackageJson } });
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ sandpackReady: true });
wrapper.vm.update();
2022-04-04 11:22:00 +05:30
return waitForPromises().then(() => {
2019-12-04 20:38:33 +05:30
expect(smooshpack.Manager).toHaveBeenCalled();
});
});
it('calls updatePreview', () => {
2021-03-08 18:12:59 +05:30
createInitializedComponent();
2019-12-04 20:38:33 +05:30
wrapper.vm.update();
2021-03-08 18:12:59 +05:30
expect(wrapper.vm.manager.updatePreview).toHaveBeenCalledWith(wrapper.vm.sandboxOpts);
});
});
describe('on ide.files.change event', () => {
beforeEach(() => {
createInitializedComponent();
eventHub.$emit('ide.files.change');
});
it('calls updatePreview', () => {
2019-12-04 20:38:33 +05:30
expect(wrapper.vm.manager.updatePreview).toHaveBeenCalledWith(wrapper.vm.sandboxOpts);
});
});
});
describe('template', () => {
2022-04-04 11:22:00 +05:30
it('renders ide-preview element when showPreview is true', async () => {
2019-12-04 20:38:33 +05:30
createComponent({ getters: { packageJson: dummyPackageJson } });
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: false });
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.find('#ide-preview').exists()).toBe(true);
2019-12-04 20:38:33 +05:30
});
2022-04-04 11:22:00 +05:30
it('renders empty state', async () => {
2019-12-04 20:38:33 +05:30
createComponent();
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: false });
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.text()).toContain(
'Preview your web application using Web IDE client-side evaluation.',
);
2019-12-04 20:38:33 +05:30
});
2022-04-04 11:22:00 +05:30
it('renders loading icon', async () => {
2019-12-04 20:38:33 +05:30
createComponent();
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2019-12-04 20:38:33 +05:30
wrapper.setData({ loading: true });
2022-04-04 11:22:00 +05:30
await nextTick();
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
2019-12-04 20:38:33 +05:30
});
});
2021-03-08 18:12:59 +05:30
describe('when destroyed', () => {
let spy;
beforeEach(() => {
createInitializedComponent();
spy = wrapper.vm.manager.updatePreview;
wrapper.destroy();
});
it('does not call updatePreview', () => {
expect(spy).not.toHaveBeenCalled();
});
});
2019-12-04 20:38:33 +05:30
});