2019-11-16 16:06:10 -05:00
|
|
|
import axios from 'axios';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-03-12 07:09:33 -05:00
|
|
|
import getIdeProject from 'ee_else_ce/ide/queries/get_ide_project.query.graphql';
|
2019-06-17 13:53:58 -04:00
|
|
|
import Api from '~/api';
|
2021-05-07 17:10:34 -04:00
|
|
|
import dismissUserCallout from '~/graphql_shared/mutations/dismiss_user_callout.mutation.graphql';
|
2021-02-14 13:09:20 -05:00
|
|
|
import services from '~/ide/services';
|
2021-05-07 17:10:34 -04:00
|
|
|
import { query, mutate } from '~/ide/services/gql';
|
2019-12-16 19:07:59 -05:00
|
|
|
import { escapeFileUrl } from '~/lib/utils/url_utility';
|
2021-05-07 17:10:34 -04:00
|
|
|
import ciConfig from '~/pipeline_editor/graphql/queries/ci_config.graphql';
|
2020-02-14 10:09:08 -05:00
|
|
|
import { projectData } from '../mock_data';
|
2019-06-17 13:53:58 -04:00
|
|
|
|
|
|
|
jest.mock('~/api');
|
2020-02-14 10:09:08 -05:00
|
|
|
jest.mock('~/ide/services/gql');
|
2019-06-17 13:53:58 -04:00
|
|
|
|
2020-02-14 10:09:08 -05:00
|
|
|
const TEST_NAMESPACE = 'alice';
|
|
|
|
const TEST_PROJECT = 'wonderland';
|
|
|
|
const TEST_PROJECT_ID = `${TEST_NAMESPACE}/${TEST_PROJECT}`;
|
2021-06-14 20:10:11 -04:00
|
|
|
const TEST_BRANCH = 'main-patch-123';
|
2019-06-17 13:53:58 -04:00
|
|
|
const TEST_COMMIT_SHA = '123456789';
|
2019-11-16 16:06:10 -05:00
|
|
|
const TEST_FILE_PATH = 'README2.md';
|
|
|
|
const TEST_FILE_OLD_PATH = 'OLD_README2.md';
|
|
|
|
const TEST_FILE_PATH_SPECIAL = 'READM?ME/abc';
|
|
|
|
const TEST_FILE_CONTENTS = 'raw file content';
|
2019-06-17 13:53:58 -04:00
|
|
|
|
|
|
|
describe('IDE services', () => {
|
|
|
|
describe('commit', () => {
|
|
|
|
let payload;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
payload = {
|
|
|
|
branch: TEST_BRANCH,
|
|
|
|
commit_message: 'Hello world',
|
|
|
|
actions: [],
|
2019-06-26 10:07:56 -04:00
|
|
|
start_sha: TEST_COMMIT_SHA,
|
2019-06-17 13:53:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Api.commitMultiple.mockReturnValue(Promise.resolve());
|
|
|
|
});
|
|
|
|
|
2019-06-26 10:07:56 -04:00
|
|
|
it('should commit', () => {
|
|
|
|
services.commit(TEST_PROJECT_ID, payload);
|
2019-06-17 13:53:58 -04:00
|
|
|
|
2019-06-26 10:07:56 -04:00
|
|
|
expect(Api.commitMultiple).toHaveBeenCalledWith(TEST_PROJECT_ID, payload);
|
2019-06-17 13:53:58 -04:00
|
|
|
});
|
|
|
|
});
|
2019-11-16 16:06:10 -05:00
|
|
|
|
2020-04-10 08:09:36 -04:00
|
|
|
describe('getRawFileData', () => {
|
|
|
|
it("resolves with a file's content if its a tempfile and it isn't renamed", () => {
|
|
|
|
const file = {
|
|
|
|
path: 'file',
|
|
|
|
tempFile: true,
|
|
|
|
content: 'content',
|
|
|
|
raw: 'raw content',
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getRawFileData(file).then((raw) => {
|
2020-04-10 08:09:36 -04:00
|
|
|
expect(raw).toBe('content');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves with file.raw if the file is renamed', () => {
|
|
|
|
const file = {
|
|
|
|
path: 'file',
|
|
|
|
tempFile: true,
|
|
|
|
content: 'content',
|
|
|
|
prevPath: 'old_path',
|
|
|
|
raw: 'raw content',
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getRawFileData(file).then((raw) => {
|
2020-04-10 08:09:36 -04:00
|
|
|
expect(raw).toBe('raw content');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns file.raw if it exists', () => {
|
|
|
|
const file = {
|
|
|
|
path: 'file',
|
|
|
|
content: 'content',
|
|
|
|
raw: 'raw content',
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getRawFileData(file).then((raw) => {
|
2020-04-10 08:09:36 -04:00
|
|
|
expect(raw).toBe('raw content');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns file.raw if file.raw is empty but file.rawPath doesn't exist", () => {
|
|
|
|
const file = {
|
|
|
|
path: 'file',
|
|
|
|
content: 'content',
|
|
|
|
raw: '',
|
|
|
|
};
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getRawFileData(file).then((raw) => {
|
2020-04-10 08:09:36 -04:00
|
|
|
expect(raw).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("if file.rawPath exists but file.raw doesn't exist", () => {
|
|
|
|
let file;
|
|
|
|
let mock;
|
|
|
|
beforeEach(() => {
|
|
|
|
file = {
|
|
|
|
path: 'file',
|
|
|
|
content: 'content',
|
|
|
|
raw: '',
|
|
|
|
rawPath: 'some_raw_path',
|
|
|
|
};
|
|
|
|
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
mock.onGet(file.rawPath).reply(200, 'raw content');
|
|
|
|
|
|
|
|
jest.spyOn(axios, 'get');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends a request to file.rawPath', () => {
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getRawFileData(file).then((raw) => {
|
2020-12-17 19:10:04 -05:00
|
|
|
expect(axios.get).toHaveBeenCalledWith(file.rawPath, {
|
|
|
|
transformResponse: [expect.any(Function)],
|
|
|
|
});
|
|
|
|
expect(raw).toEqual('raw content');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns arraybuffer for binary files', () => {
|
|
|
|
file.binary = true;
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getRawFileData(file).then((raw) => {
|
2020-12-17 19:10:04 -05:00
|
|
|
expect(axios.get).toHaveBeenCalledWith(file.rawPath, {
|
|
|
|
transformResponse: [expect.any(Function)],
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
});
|
2020-04-10 08:09:36 -04:00
|
|
|
expect(raw).toEqual('raw content');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-16 16:06:10 -05:00
|
|
|
describe('getBaseRawFileData', () => {
|
|
|
|
let file;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
file = {
|
|
|
|
mrChange: null,
|
|
|
|
projectId: TEST_PROJECT_ID,
|
|
|
|
path: TEST_FILE_PATH,
|
|
|
|
};
|
|
|
|
|
|
|
|
jest.spyOn(axios, 'get');
|
|
|
|
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gives back file.baseRaw for files with that property present', () => {
|
|
|
|
file.baseRaw = TEST_FILE_CONTENTS;
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getBaseRawFileData(file, TEST_PROJECT_ID, TEST_COMMIT_SHA).then((content) => {
|
2019-11-16 16:06:10 -05:00
|
|
|
expect(content).toEqual(TEST_FILE_CONTENTS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gives back file.baseRaw for files for temp files', () => {
|
|
|
|
file.tempFile = true;
|
|
|
|
file.baseRaw = TEST_FILE_CONTENTS;
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getBaseRawFileData(file, TEST_PROJECT_ID, TEST_COMMIT_SHA).then((content) => {
|
2019-11-16 16:06:10 -05:00
|
|
|
expect(content).toEqual(TEST_FILE_CONTENTS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
|
|
|
relativeUrlRoot | filePath | isRenamed
|
|
|
|
${''} | ${TEST_FILE_PATH} | ${false}
|
|
|
|
${''} | ${TEST_FILE_OLD_PATH} | ${true}
|
|
|
|
${''} | ${TEST_FILE_PATH_SPECIAL} | ${false}
|
|
|
|
${''} | ${TEST_FILE_PATH_SPECIAL} | ${true}
|
|
|
|
${'gitlab'} | ${TEST_FILE_OLD_PATH} | ${true}
|
|
|
|
`(
|
|
|
|
'with relativeUrlRoot ($relativeUrlRoot) and filePath ($filePath) and isRenamed ($isRenamed)',
|
|
|
|
({ relativeUrlRoot, filePath, isRenamed }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
if (isRenamed) {
|
|
|
|
file.mrChange = {
|
|
|
|
renamed_file: true,
|
|
|
|
old_path: filePath,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
file.path = filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
gon.relative_url_root = relativeUrlRoot;
|
|
|
|
|
|
|
|
mock
|
|
|
|
.onGet(
|
2020-01-27 07:08:35 -05:00
|
|
|
`${relativeUrlRoot}/${TEST_PROJECT_ID}/-/raw/${TEST_COMMIT_SHA}/${escapeFileUrl(
|
2019-11-16 16:06:10 -05:00
|
|
|
filePath,
|
|
|
|
)}`,
|
|
|
|
)
|
|
|
|
.reply(200, TEST_FILE_CONTENTS);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches file content', () =>
|
2020-12-23 16:10:24 -05:00
|
|
|
services.getBaseRawFileData(file, TEST_PROJECT_ID, TEST_COMMIT_SHA).then((content) => {
|
2019-11-16 16:06:10 -05:00
|
|
|
expect(content).toEqual(TEST_FILE_CONTENTS);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2020-02-14 10:09:08 -05:00
|
|
|
|
|
|
|
describe('getProjectData', () => {
|
|
|
|
it('combines gql and API requests', () => {
|
|
|
|
const gqlProjectData = {
|
|
|
|
userPermissions: {
|
|
|
|
bogus: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
Api.project.mockReturnValue(Promise.resolve({ data: { ...projectData } }));
|
2020-07-09 08:08:56 -04:00
|
|
|
query.mockReturnValue(Promise.resolve({ data: { project: gqlProjectData } }));
|
2020-02-14 10:09:08 -05:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
return services.getProjectData(TEST_NAMESPACE, TEST_PROJECT).then((response) => {
|
2020-02-14 10:09:08 -05:00
|
|
|
expect(response).toEqual({ data: { ...projectData, ...gqlProjectData } });
|
|
|
|
expect(Api.project).toHaveBeenCalledWith(TEST_PROJECT_ID);
|
2020-07-09 08:08:56 -04:00
|
|
|
expect(query).toHaveBeenCalledWith({
|
2021-03-12 07:09:33 -05:00
|
|
|
query: getIdeProject,
|
2020-02-14 10:09:08 -05:00
|
|
|
variables: {
|
|
|
|
projectPath: TEST_PROJECT_ID,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-11 05:09:45 -04:00
|
|
|
|
|
|
|
describe('getFiles', () => {
|
|
|
|
let mock;
|
|
|
|
let relativeUrlRoot;
|
|
|
|
const TEST_RELATIVE_URL_ROOT = 'blah-blah';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(axios, 'get');
|
|
|
|
relativeUrlRoot = gon.relative_url_root;
|
|
|
|
gon.relative_url_root = TEST_RELATIVE_URL_ROOT;
|
|
|
|
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
mock
|
|
|
|
.onGet(`${TEST_RELATIVE_URL_ROOT}/${TEST_PROJECT_ID}/-/files/${TEST_COMMIT_SHA}`)
|
|
|
|
.reply(200, [TEST_FILE_PATH]);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
gon.relative_url_root = relativeUrlRoot;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('initates the api call based on the passed path and commit hash', () => {
|
|
|
|
return services.getFiles(TEST_PROJECT_ID, TEST_COMMIT_SHA).then(({ data }) => {
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(
|
|
|
|
`${gon.relative_url_root}/${TEST_PROJECT_ID}/-/files/${TEST_COMMIT_SHA}`,
|
|
|
|
expect.any(Object),
|
|
|
|
);
|
|
|
|
expect(data).toEqual([TEST_FILE_PATH]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-13 20:07:47 -04:00
|
|
|
|
|
|
|
describe('pingUsage', () => {
|
|
|
|
let mock;
|
|
|
|
let relativeUrlRoot;
|
|
|
|
const TEST_RELATIVE_URL_ROOT = 'blah-blah';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(axios, 'post');
|
|
|
|
relativeUrlRoot = gon.relative_url_root;
|
|
|
|
gon.relative_url_root = TEST_RELATIVE_URL_ROOT;
|
|
|
|
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
gon.relative_url_root = relativeUrlRoot;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('posts to usage endpoint', () => {
|
|
|
|
const TEST_PROJECT_PATH = 'foo/bar';
|
2021-06-24 08:08:07 -04:00
|
|
|
const axiosURL = `${TEST_RELATIVE_URL_ROOT}/${TEST_PROJECT_PATH}/service_ping/web_ide_pipelines_count`;
|
2020-05-13 20:07:47 -04:00
|
|
|
|
|
|
|
mock.onPost(axiosURL).reply(200);
|
|
|
|
|
|
|
|
return services.pingUsage(TEST_PROJECT_PATH).then(() => {
|
|
|
|
expect(axios.post).toHaveBeenCalledWith(axiosURL);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-05-07 17:10:34 -04:00
|
|
|
describe('getCiConfig', () => {
|
|
|
|
const TEST_PROJECT_PATH = 'foo/bar';
|
|
|
|
const TEST_CI_CONFIG = 'test config';
|
|
|
|
|
|
|
|
it('queries with the given CI config and project', () => {
|
|
|
|
const result = { data: { ciConfig: { test: 'data' } } };
|
|
|
|
query.mockResolvedValue(result);
|
|
|
|
return services.getCiConfig(TEST_PROJECT_PATH, TEST_CI_CONFIG).then((data) => {
|
|
|
|
expect(data).toEqual(result.data.ciConfig);
|
|
|
|
expect(query).toHaveBeenCalledWith({
|
|
|
|
query: ciConfig,
|
|
|
|
variables: { projectPath: TEST_PROJECT_PATH, content: TEST_CI_CONFIG },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('dismissUserCallout', () => {
|
|
|
|
it('mutates the callout to dismiss', () => {
|
|
|
|
const result = { data: { callouts: { test: 'data' } } };
|
|
|
|
mutate.mockResolvedValue(result);
|
|
|
|
return services.dismissUserCallout('test').then((data) => {
|
|
|
|
expect(data).toEqual(result.data);
|
|
|
|
expect(mutate).toHaveBeenCalledWith({
|
|
|
|
mutation: dismissUserCallout,
|
|
|
|
variables: { input: { featureName: 'test' } },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-06-17 13:53:58 -04:00
|
|
|
});
|