Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track when a custom type is saved #468

Merged
merged 4 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import React from "react";
import { Box, Button, Spinner, Text } from "theme-ui";

import Header from "../../../../components/Header";
import useSliceMachineActions from "@src/modules/useSliceMachineActions";
Heyrows marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the first time I see a change of import in your PR, is there a specific reason to do that ?

I mean it's not like the code didn't work before so is it because you prefer relative path ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple reasons.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • sometimes the alias path is long ✅
  • my ide doesn't work with them 🟥 , pretty sure your IDE should be able to do that
  • use of module alias, I do agree that is not a good practice.

Overall, if the path is long I would say ok but here replacing "@src" with "../../../../" doesn't seems right and honestly the aliases looses their purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my ide doesn't work with them 🟥 , pretty sure your IDE should be able to do that
Well it doesn't.

import useSliceMachineActions from "../../../../src/modules/useSliceMachineActions";
import { MdSpaceDashboard } from "react-icons/md";
import { useSelector } from "react-redux";
import { SliceMachineStoreType } from "@src/redux/type";
import { SliceMachineStoreType } from "../../../../src/redux/type";
import {
selectCurrentCustomType,
selectCustomTypeStatus,
selectIsCurrentCustomTypeHasPendingModifications,
} from "@src/modules/selectedCustomType";
} from "../../../../src/modules/selectedCustomType";
import { isLoading } from "@src/modules/loading";
import { LoadingKeysEnum } from "@src/modules/loading/types";
import { CustomTypeStatus } from "@src/modules/selectedCustomType/types";
import { CustomTypeStatus } from "../../../../src/modules/selectedCustomType/types";

const CustomTypeHeader = () => {
const {
Expand Down
28 changes: 14 additions & 14 deletions packages/slice-machine/src/modules/selectedCustomType/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { call, fork, put, select, takeLatest } from "redux-saga/effects";
import { openToasterCreator, ToasterType } from "@src/modules/toaster";
import { openToasterCreator, ToasterType } from "../toaster";
import { getType } from "typesafe-actions";
import { withLoader } from "@src/modules/loading";
import { LoadingKeysEnum } from "@src/modules/loading/types";
import {
pushCustomTypeCreator,
saveCustomTypeCreator,
} from "@src/modules/selectedCustomType/actions";
import {
selectCurrentCustomType,
selectCurrentMockConfig,
} from "@src/modules/selectedCustomType/index";
import { pushCustomType, saveCustomType } from "@src/apiClient";
import { withLoader } from "../loading";
import { LoadingKeysEnum } from "../loading/types";
import { pushCustomTypeCreator, saveCustomTypeCreator } from "./actions";
import { selectCurrentCustomType, selectCurrentMockConfig } from "./index";
import { pushCustomType, saveCustomType } from "../../../src/apiClient";
import axios from "axios";
import { modalOpenCreator } from "@src/modules/modal";
import { ModalKeysEnum } from "@src/modules/modal/types";
import { modalOpenCreator } from "../modal";
import { ModalKeysEnum } from "../modal/types";
import Tracker from "../../../src/tracker";

export function* saveCustomTypeSaga() {
try {
Expand All @@ -30,6 +25,11 @@ export function* saveCustomTypeSaga() {
}

yield call(saveCustomType, currentCustomType, currentMockConfig);
void Tracker.get().trackCustomTypeSaved({
Heyrows marked this conversation as resolved.
Show resolved Hide resolved
Heyrows marked this conversation as resolved.
Show resolved Hide resolved
id: currentCustomType.id,
name: currentCustomType.label || currentCustomType.id,
type: currentCustomType.repeatable ? "repeatable" : "single",
});
yield put(saveCustomTypeCreator.success());
yield put(
openToasterCreator({
Expand Down
25 changes: 11 additions & 14 deletions packages/slice-machine/src/modules/useSliceMachineActions.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import { useDispatch } from "react-redux";
import { LoadingKeysEnum } from "@src/modules/loading/types";
import { ModalKeysEnum } from "@src/modules/modal/types";
import { modalCloseCreator, modalOpenCreator } from "@src/modules/modal";
import {
startLoadingActionCreator,
stopLoadingActionCreator,
} from "@src/modules/loading";
import { LoadingKeysEnum } from "./loading/types";
import { ModalKeysEnum } from "./modal/types";
import { modalCloseCreator, modalOpenCreator } from "./modal";
import { startLoadingActionCreator, stopLoadingActionCreator } from "./loading";
import {
finishOnboardingCreator,
sendAReviewCreator,
skipReviewCreator,
updatesViewedCreator,
hasSeenTutorialsTooTipCreator,
} from "@src/modules/userContext";
import { refreshStateCreator } from "@src/modules/environment";
} from "./userContext";
import { refreshStateCreator } from "./environment";
import {
openSetupDrawerCreator,
closeSetupDrawerCreator,
toggleSetupDrawerStepCreator,
checkSimulatorSetupCreator,
connectToSimulatorIframeCreator,
} from "@src/modules/simulator";
} from "./simulator";
import ServerState from "@models/server/ServerState";
import { createCustomTypeCreator } from "@src/modules/availableCustomTypes";
Heyrows marked this conversation as resolved.
Show resolved Hide resolved
Heyrows marked this conversation as resolved.
Show resolved Hide resolved
import { createSliceCreator } from "@src/modules/slices";
import { createCustomTypeCreator } from "./availableCustomTypes";
import { createSliceCreator } from "./slices";
import { UserContextStoreType } from "./userContext/types";
import { openToasterCreator, ToasterType } from "@src/modules/toaster";
import { openToasterCreator, ToasterType } from "./toaster";
import {
initCustomTypeStoreCreator,
createTabCreator,
Expand All @@ -48,7 +45,7 @@ import {
deleteGroupFieldMockConfigCreator,
deleteFieldMockConfigCreator,
updateFieldMockConfigCreator,
} from "@src/modules/selectedCustomType";
} from "./selectedCustomType";
import { CustomTypeMockConfig } from "@models/common/MockConfig";
import {
CustomTypeSM,
Expand Down
9 changes: 9 additions & 0 deletions packages/slice-machine/src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum EventType {
CreateCustomType = "SliceMachine Custom Type Created",
CustomTypeFieldAdded = "SliceMachine Custom Type Field Added",
CustomTypeSliceZoneUpdated = "SliceMachine Slicezone Updated",
CustomTypeSaved = "SliceMachine Custom Type Saved",
}

export enum ContinueOnboardingType {
Expand Down Expand Up @@ -240,6 +241,14 @@ export class SMTracker {
}): Promise<void> {
return this.#trackEvent(EventType.CustomTypeSliceZoneUpdated, data);
}

async trackCustomTypeSaved(data: {
id: string;
name: string;
type: "single" | "repeatable";
}): Promise<void> {
return this.#trackEvent(EventType.CustomTypeSaved, data);
}
}

const Tracker = (() => {
Expand Down
204 changes: 203 additions & 1 deletion packages/slice-machine/tests/pages/cts.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,31 @@ import {
beforeEach,
expect,
beforeAll,
afterAll,
} from "@jest/globals";
import React from "react";
import CreateCustomTypeBuilder from "../../pages/cts/[ct]";
import singletonRouter from "next/router";
import { render, fireEvent, act, screen } from "../test-utils";
import { render, fireEvent, act, screen, waitFor } from "../test-utils";
import mockRouter from "next-router-mock";
import { AnalyticsBrowser } from "@segment/analytics-next";
import Tracker from "../../src/tracker";
import LibrariesProvider from "../../src/models/libraries/context";
import { setupServer } from "msw/node";
import { rest } from "msw";

jest.mock("next/dist/client/router", () => require("next-router-mock"));

const server = setupServer(
rest.post("/api/custom-types/save", (_, res, ctx) => {
return res(ctx.json({}));
})
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe("Custom Type Builder", () => {
const fakeTracker = jest.fn().mockImplementation(() => Promise.resolve());

Expand Down Expand Up @@ -323,4 +336,193 @@ describe("Custom Type Builder", () => {
{ context: { groupId: { Repository: "repoName" } } }
);
});

test("it should send a tracking event when the user saves a custoom-type", async () => {
const customTypeId = "a-page";

singletonRouter.push({
pathname: "cts/[ct]",
query: { ct: customTypeId },
});

const App = render(<CreateCustomTypeBuilder />, {
preloadedState: {
environment: {
framework: "next",
mockConfig: { _cts: { [customTypeId]: {} } },
},
availableCustomTypes: {
[customTypeId]: {
local: {
id: customTypeId,
label: customTypeId,
repeatable: true,
status: true,
tabs: [
{
key: "Main",
value: [],
},
],
},
},
},
selectedCustomType: {
model: {
id: "a-page",
label: "a-page",
repeatable: true,
status: true,
tabs: [
{
key: "Main",
value: [],
},
],
},
initialModel: {
id: "a-page",
label: "a-page",
repeatable: true,
status: true,
tabs: [
{
key: "Main",
value: [],
},
],
},
mockConfig: {},
initialMockConfig: {},
},
},
});

const addButton = screen.getByTestId("empty-zone-add-new-field");
fireEvent.click(addButton);

const uid = screen.getByText("UID");
fireEvent.click(uid);

const saveFieldButton = screen.getByText("Add");

await act(async () => {
fireEvent.click(saveFieldButton);
});

expect(fakeTracker).toHaveBeenCalledWith(
"SliceMachine Custom Type Field Added",
{ id: "uid", name: customTypeId, type: "UID", zone: "static" },
{ context: { groupId: { Repository: "repoName" } } }
);

const saveCustomType = screen.getByText("Save to File System");

await act(async () => {
fireEvent.click(saveCustomType);
});

await waitFor(() => {
expect(fakeTracker).toHaveBeenLastCalledWith(
"SliceMachine Custom Type Saved",
{ type: "repeatable", id: customTypeId, name: customTypeId },
{ context: { groupId: { Repository: "repoName" } } }
);
});
});

test("if saving fails a it should not send the save event", async () => {
server.use(
rest.post("/api/custom-types/save", (_, res, ctx) => {
return res(ctx.status(500), ctx.json({}));
})
);
const customTypeId = "a-page";

singletonRouter.push({
pathname: "cts/[ct]",
query: { ct: customTypeId },
});

const App = render(<CreateCustomTypeBuilder />, {
preloadedState: {
environment: {
framework: "next",
mockConfig: { _cts: { [customTypeId]: {} } },
},
availableCustomTypes: {
[customTypeId]: {
local: {
id: customTypeId,
label: customTypeId,
repeatable: true,
status: true,
tabs: [
{
key: "Main",
value: [],
},
],
},
},
},
selectedCustomType: {
model: {
id: "a-page",
label: "a-page",
repeatable: true,
status: true,
tabs: [
{
key: "Main",
value: [],
},
],
},
initialModel: {
id: "a-page",
label: "a-page",
repeatable: true,
status: true,
tabs: [
{
key: "Main",
value: [],
},
],
},
mockConfig: {},
initialMockConfig: {},
},
},
});

const addButton = screen.getByTestId("empty-zone-add-new-field");
fireEvent.click(addButton);

const uid = screen.getByText("UID");
fireEvent.click(uid);

const saveFieldButton = screen.getByText("Add");

await act(async () => {
fireEvent.click(saveFieldButton);
});

expect(fakeTracker).toHaveBeenCalledWith(
Heyrows marked this conversation as resolved.
Show resolved Hide resolved
"SliceMachine Custom Type Field Added",
{ id: "uid", name: customTypeId, type: "UID", zone: "static" },
{ context: { groupId: { Repository: "repoName" } } }
);

const saveCustomType = screen.getByText("Save to File System");
Heyrows marked this conversation as resolved.
Show resolved Hide resolved

await act(async () => {
fireEvent.click(saveCustomType);
});

await new Promise((r) => setTimeout(r, 1000));

expect(fakeTracker).toHaveBeenCalledTimes(1);
});
});