Commit 5b9e57e0 authored by Derek Knox's avatar Derek Knox Committed by Paul Slaughter
parent ab2b2108
...@@ -15,3 +15,5 @@ export const LOAD_CONTENT_ERROR = __( ...@@ -15,3 +15,5 @@ export const LOAD_CONTENT_ERROR = __(
); );
export const DEFAULT_HEADING = s__('StaticSiteEditor|Static site editor'); export const DEFAULT_HEADING = s__('StaticSiteEditor|Static site editor');
export const TRACKING_ACTION_CREATE_COMMIT = 'create_commit';
import Api from '~/api'; import Api from '~/api';
import Tracking from '~/tracking';
import { s__, sprintf } from '~/locale'; import { s__, sprintf } from '~/locale';
import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils';
import generateBranchName from '~/static_site_editor/services/generate_branch_name'; import generateBranchName from '~/static_site_editor/services/generate_branch_name';
...@@ -8,6 +9,7 @@ import { ...@@ -8,6 +9,7 @@ import {
SUBMIT_CHANGES_BRANCH_ERROR, SUBMIT_CHANGES_BRANCH_ERROR,
SUBMIT_CHANGES_COMMIT_ERROR, SUBMIT_CHANGES_COMMIT_ERROR,
SUBMIT_CHANGES_MERGE_REQUEST_ERROR, SUBMIT_CHANGES_MERGE_REQUEST_ERROR,
TRACKING_ACTION_CREATE_COMMIT,
} from '../constants'; } from '../constants';
const createBranch = (projectId, branch) => const createBranch = (projectId, branch) =>
...@@ -18,8 +20,10 @@ const createBranch = (projectId, branch) => ...@@ -18,8 +20,10 @@ const createBranch = (projectId, branch) =>
throw new Error(SUBMIT_CHANGES_BRANCH_ERROR); throw new Error(SUBMIT_CHANGES_BRANCH_ERROR);
}); });
const commitContent = (projectId, message, branch, sourcePath, content) => const commitContent = (projectId, message, branch, sourcePath, content) => {
Api.commitMultiple( Tracking.event(document.body.dataset.page, TRACKING_ACTION_CREATE_COMMIT);
return Api.commitMultiple(
projectId, projectId,
convertObjectPropsToSnakeCase({ convertObjectPropsToSnakeCase({
branch, branch,
...@@ -35,6 +39,7 @@ const commitContent = (projectId, message, branch, sourcePath, content) => ...@@ -35,6 +39,7 @@ const commitContent = (projectId, message, branch, sourcePath, content) =>
).catch(() => { ).catch(() => {
throw new Error(SUBMIT_CHANGES_COMMIT_ERROR); throw new Error(SUBMIT_CHANGES_COMMIT_ERROR);
}); });
};
const createMergeRequest = (projectId, title, sourceBranch, targetBranch = DEFAULT_TARGET_BRANCH) => const createMergeRequest = (projectId, title, sourceBranch, targetBranch = DEFAULT_TARGET_BRANCH) =>
Api.createProjectMergeRequest( Api.createProjectMergeRequest(
......
...@@ -45,3 +45,5 @@ export const createMergeRequestResponse = { ...@@ -45,3 +45,5 @@ export const createMergeRequestResponse = {
iid: '123', iid: '123',
web_url: '/merge_requests/123', web_url: '/merge_requests/123',
}; };
export const trackingCategory = 'projects:static_site_editor:show';
import Api from '~/api'; import Api from '~/api';
import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToSnakeCase } from '~/lib/utils/common_utils';
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import { import {
DEFAULT_TARGET_BRANCH, DEFAULT_TARGET_BRANCH,
SUBMIT_CHANGES_BRANCH_ERROR, SUBMIT_CHANGES_BRANCH_ERROR,
SUBMIT_CHANGES_COMMIT_ERROR, SUBMIT_CHANGES_COMMIT_ERROR,
SUBMIT_CHANGES_MERGE_REQUEST_ERROR, SUBMIT_CHANGES_MERGE_REQUEST_ERROR,
TRACKING_ACTION_CREATE_COMMIT,
} from '~/static_site_editor/constants'; } from '~/static_site_editor/constants';
import generateBranchName from '~/static_site_editor/services/generate_branch_name'; import generateBranchName from '~/static_site_editor/services/generate_branch_name';
import submitContentChanges from '~/static_site_editor/services/submit_content_changes'; import submitContentChanges from '~/static_site_editor/services/submit_content_changes';
...@@ -18,6 +20,7 @@ import { ...@@ -18,6 +20,7 @@ import {
createMergeRequestResponse, createMergeRequestResponse,
sourcePath, sourcePath,
sourceContent as content, sourceContent as content,
trackingCategory,
} from '../mock_data'; } from '../mock_data';
jest.mock('~/static_site_editor/services/generate_branch_name'); jest.mock('~/static_site_editor/services/generate_branch_name');
...@@ -25,6 +28,8 @@ jest.mock('~/static_site_editor/services/generate_branch_name'); ...@@ -25,6 +28,8 @@ jest.mock('~/static_site_editor/services/generate_branch_name');
describe('submitContentChanges', () => { describe('submitContentChanges', () => {
const mergeRequestTitle = `Update ${sourcePath} file`; const mergeRequestTitle = `Update ${sourcePath} file`;
const branch = 'branch-name'; const branch = 'branch-name';
let trackingSpy;
let origPage;
beforeEach(() => { beforeEach(() => {
jest.spyOn(Api, 'createBranch').mockResolvedValue({ data: commitBranchResponse }); jest.spyOn(Api, 'createBranch').mockResolvedValue({ data: commitBranchResponse });
...@@ -34,6 +39,15 @@ describe('submitContentChanges', () => { ...@@ -34,6 +39,15 @@ describe('submitContentChanges', () => {
.mockResolvedValue({ data: createMergeRequestResponse }); .mockResolvedValue({ data: createMergeRequestResponse });
generateBranchName.mockReturnValue(branch); generateBranchName.mockReturnValue(branch);
origPage = document.body.dataset.page;
document.body.dataset.page = trackingCategory;
trackingSpy = mockTracking(document.body.dataset.page, undefined, jest.spyOn);
});
afterEach(() => {
document.body.dataset.page = origPage;
unmockTracking();
}); });
it('creates a branch named after the username and target branch', () => { it('creates a branch named after the username and target branch', () => {
...@@ -69,6 +83,15 @@ describe('submitContentChanges', () => { ...@@ -69,6 +83,15 @@ describe('submitContentChanges', () => {
}); });
}); });
it('sends the correct tracking event when committing content changes', () => {
return submitContentChanges({ username, projectId, sourcePath, content }).then(() => {
expect(trackingSpy).toHaveBeenCalledWith(
document.body.dataset.page,
TRACKING_ACTION_CREATE_COMMIT,
);
});
});
it('notifies error when content could not be committed', () => { it('notifies error when content could not be committed', () => {
Api.commitMultiple.mockRejectedValueOnce(); Api.commitMultiple.mockRejectedValueOnce();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment