diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-14 12:09:34 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-14 12:09:34 +0300 |
commit | 97f0ae7454597105a27df65ffb772949d9d4f3cb (patch) | |
tree | 0bf4888e0e9082c8f168a211390a73a6ae810cef /spec/frontend/static_site_editor | |
parent | 5ebc4d92cd5fbb46c627eb04d500384893dbe2b4 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/static_site_editor')
3 files changed, 161 insertions, 0 deletions
diff --git a/spec/frontend/static_site_editor/mock_data.js b/spec/frontend/static_site_editor/mock_data.js index 9e1c14515e6..1993636ab12 100644 --- a/spec/frontend/static_site_editor/mock_data.js +++ b/spec/frontend/static_site_editor/mock_data.js @@ -34,3 +34,11 @@ export const savedContentMeta = { }; export const submitChangesError = 'Could not save changes'; +export const commitMultipleResponse = { + short_id: 'ed899a2f4b5', + web_url: '/commit/ed899a2f4b5', +}; +export const createMergeRequestResponse = { + iid: '123', + web_url: '/merge_requests/123', +}; diff --git a/spec/frontend/static_site_editor/services/generate_branch_name_spec.js b/spec/frontend/static_site_editor/services/generate_branch_name_spec.js new file mode 100644 index 00000000000..0624fc3b7b4 --- /dev/null +++ b/spec/frontend/static_site_editor/services/generate_branch_name_spec.js @@ -0,0 +1,22 @@ +import { DEFAULT_TARGET_BRANCH, BRANCH_SUFFIX_COUNT } from '~/static_site_editor/constants'; +import generateBranchName from '~/static_site_editor/services/generate_branch_name'; + +import { username } from '../mock_data'; + +describe('generateBranchName', () => { + const timestamp = 12345678901234; + + beforeEach(() => { + jest.spyOn(Date, 'now').mockReturnValueOnce(timestamp); + }); + + it('generates a name that includes the username and target branch', () => { + expect(generateBranchName(username)).toMatch(`${username}-${DEFAULT_TARGET_BRANCH}`); + }); + + it(`adds the first ${BRANCH_SUFFIX_COUNT} numbers of the current timestamp`, () => { + expect(generateBranchName(username)).toMatch( + timestamp.toString().substring(BRANCH_SUFFIX_COUNT), + ); + }); +}); diff --git a/spec/frontend/static_site_editor/services/submit_content_changes_spec.js b/spec/frontend/static_site_editor/services/submit_content_changes_spec.js new file mode 100644 index 00000000000..9a0bd88b57d --- /dev/null +++ b/spec/frontend/static_site_editor/services/submit_content_changes_spec.js @@ -0,0 +1,131 @@ +import Api from '~/api'; +import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils'; + +import { + DEFAULT_TARGET_BRANCH, + SUBMIT_CHANGES_BRANCH_ERROR, + SUBMIT_CHANGES_COMMIT_ERROR, + SUBMIT_CHANGES_MERGE_REQUEST_ERROR, +} from '~/static_site_editor/constants'; +import generateBranchName from '~/static_site_editor/services/generate_branch_name'; +import submitContentChanges from '~/static_site_editor/services/submit_content_changes'; + +import { + username, + projectId, + commitMultipleResponse, + createMergeRequestResponse, + sourcePath, + sourceContent as content, +} from '../mock_data'; + +jest.mock('~/static_site_editor/services/generate_branch_name'); + +describe('submitContentChanges', () => { + const mergeRequestTitle = `Update ${sourcePath} file`; + const branch = 'branch-name'; + + beforeEach(() => { + jest.spyOn(Api, 'createBranch').mockResolvedValue(); + jest.spyOn(Api, 'commitMultiple').mockResolvedValue({ data: commitMultipleResponse }); + jest + .spyOn(Api, 'createProjectMergeRequest') + .mockResolvedValue({ data: createMergeRequestResponse }); + + generateBranchName.mockReturnValue(branch); + }); + + it('creates a branch named after the username and target branch', () => { + return submitContentChanges({ username, projectId }).then(() => { + expect(Api.createBranch).toHaveBeenCalledWith(projectId, { + ref: DEFAULT_TARGET_BRANCH, + branch, + }); + }); + }); + + it('notifies error when branch could not be created', () => { + Api.createBranch.mockRejectedValueOnce(); + + expect(submitContentChanges({ username, projectId })).rejects.toThrow( + SUBMIT_CHANGES_BRANCH_ERROR, + ); + }); + + it('commits the content changes to the branch when creating branch succeeds', () => { + return submitContentChanges({ username, projectId, sourcePath, content }).then(() => { + expect(Api.commitMultiple).toHaveBeenCalledWith(projectId, { + branch, + commit_message: mergeRequestTitle, + actions: [ + { + action: 'update', + file_path: sourcePath, + content, + }, + ], + }); + }); + }); + + it('notifies error when content could not be committed', () => { + Api.commitMultiple.mockRejectedValueOnce(); + + expect(submitContentChanges({ username, projectId })).rejects.toThrow( + SUBMIT_CHANGES_COMMIT_ERROR, + ); + }); + + it('creates a merge request when commiting changes succeeds', () => { + return submitContentChanges({ username, projectId, sourcePath, content }).then(() => { + expect(Api.createProjectMergeRequest).toHaveBeenCalledWith( + projectId, + convertObjectPropsToSnakeCase({ + title: mergeRequestTitle, + targetBranch: DEFAULT_TARGET_BRANCH, + sourceBranch: branch, + }), + ); + }); + }); + + it('notifies error when merge request could not be created', () => { + Api.createProjectMergeRequest.mockRejectedValueOnce(); + + expect(submitContentChanges({ username, projectId })).rejects.toThrow( + SUBMIT_CHANGES_MERGE_REQUEST_ERROR, + ); + }); + + describe('when changes are submitted successfully', () => { + let result; + + beforeEach(() => { + return submitContentChanges({ username, projectId, sourcePath, content }).then(_result => { + result = _result; + }); + }); + + it('returns the branch name', () => { + expect(result).toMatchObject({ branch: { label: branch } }); + }); + + it('returns commit short id and web url', () => { + expect(result).toMatchObject({ + commit: { + label: commitMultipleResponse.short_id, + url: commitMultipleResponse.web_url, + }, + }); + }); + + it('returns merge request iid and web url', () => { + expect(result).toMatchObject({ + mergeRequest: { + label: createMergeRequestResponse.iid, + url: createMergeRequestResponse.web_url, + }, + }); + }); + }); +}); |