Skip to content

Commit

Permalink
test(fix test): fix test for usetransport
Browse files Browse the repository at this point in the history
  • Loading branch information
rileylnapier committed May 23, 2024
1 parent e410769 commit 728461e
Show file tree
Hide file tree
Showing 27 changed files with 259 additions and 215 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Want to use packages with other frameworks/libraries like _angular_ or _vue_? Ch

We've cleaned up a bunch of stale code that isn't being used anymore. We no longer export "Messages" from client-graphql and only support "Inbox" as a datasource.

Typings have also been fixed for client-graphql and useCourier hooks
- Typings have also been fixed for client-graphql and useCourier hooks
- Transport moved to a new package @trycourier/transport
- New package @trycourier/core to hold common types and lib functions

Styled Components is now a peer dependency

Expand Down
2 changes: 1 addition & 1 deletion packages/client-graphql/src/inbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MarkAllRead, markAllRead } from "./mark-all-read";
import { TrackEvent, trackEvent } from "./track-event";

export { IInboxMessage } from "./message";
export { IInboxMessagePreview, IGetInboxMessagesParams } from "./messages";
export { IGetInboxMessagesParams } from "./messages";

export default (
params:
Expand Down
7 changes: 2 additions & 5 deletions packages/client-graphql/src/inbox/message-lists.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
IGetInboxMessagesParams,
IInboxMessagePreview,
messagesProps,
} from "./messages";
import { IInboxMessagePreview } from "@trycourier/core";
import { IGetInboxMessagesParams, messagesProps } from "./messages";
import { Client } from "urql";

export type GetInboxMessageLists = (
Expand Down
12 changes: 1 addition & 11 deletions packages/client-graphql/src/inbox/message.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IActionElemental, ITextElemental } from "@trycourier/core";
import { Client } from "urql";

export const GET_INBOX_MESSAGE = `
Expand Down Expand Up @@ -30,17 +31,6 @@ export const GET_INBOX_MESSAGE = `
}
`;

export interface ITextElemental {
type: "text";
content: string;
}
export interface IActionElemental {
background_color?: string;
content: string;
data?: Record<string, any>;
href: string;
type: "text";
}
export interface IInboxMessage {
messageId: string;
read?: string;
Expand Down
15 changes: 1 addition & 14 deletions packages/client-graphql/src/inbox/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client } from "urql";
import { IActionElemental } from "./message";
import { IInboxMessagePreview } from "@trycourier/core";

export interface IGetInboxMessagesParams {
tenantId?: string;
Expand Down Expand Up @@ -68,19 +68,6 @@ export const createGetInboxMessagesQuery = (includePinned?: boolean) => `
}
`;

export interface IInboxMessagePreview {
actions?: IActionElemental[];
archived?: string;
created: string;
data?: Record<string, any>;
messageId: string;
opened?: string;
preview?: string;
read?: string;
tags?: string[];
title?: string;
}

export type GetInboxMessages = (
params?: IGetInboxMessagesParams,
after?: string
Expand Down
7 changes: 1 addition & 6 deletions packages/client-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ export { createCourierClient } from "./client";
export { default as Banner } from "./banner";
export { default as Brands } from "./brands";
export { default as Events } from "./events";
export {
default as Inbox,
IInboxMessagePreview,
IInboxMessage,
IGetInboxMessagesParams,
} from "./inbox";
export { default as Inbox, IGetInboxMessagesParams } from "./inbox";
export {
default as Preferences,
UpdateRecipientPreferencesPayload,
Expand Down
3 changes: 2 additions & 1 deletion packages/components/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {
} from "@testing-library/react";
import { graphql, rest } from "msw";
import { setupServer } from "msw/node";
import { IInboxMessagePreview } from "@trycourier/client-graphql";
import { IInboxMessagePreview } from "@trycourier/core";

function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const mockGraphMessage: IInboxMessagePreview = {
type: "message",
messageId: "mockMessageId",
created: new Date().toISOString(),
title: "mockTitle",
Expand Down
16 changes: 16 additions & 0 deletions packages/core/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
sourceType: "unambiguous",
plugins: [
"@babel/transform-runtime",
"transform-inline-environment-variables",
"transform-class-properties",
process.env.NODE_ENV !== "test" && [
"babel-plugin-root-import",
{
rootPathSuffix: "./src",
rootPathPrefix: "~/",
},
],
].filter(Boolean),
presets: ["@babel/preset-typescript", "@babel/preset-env"],
};
5 changes: 5 additions & 0 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const jestConfigBase = require("../../jest.config.base");
const babelConfig = require("./babel.config.js");

module.exports = jestConfigBase(babelConfig);
23 changes: 23 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@trycourier/core",
"version": "6.0.0",
"description": "",
"main": "dist/index.js",
"types": "typings/index.d.ts",
"scripts": {
"babel": "babel src -d dist --extensions \".ts\" --ignore \"src/**/__tests__/**\"",
"build:watch": "yarn babel --watch",
"build": "rimraf dist && yarn babel",
"clean": "rimraf dist && rimraf typings",
"type-check": "tsc --noEmit",
"types": "tsc --emitDeclarationOnly"
},
"license": "ISC",
"dependencies": {
"rimraf": "^3.0.2"
},
"files": [
"dist/",
"typings/"
]
}
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types";
export * from "./lib";
File renamed without changes.
146 changes: 146 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
export interface ICourierEventMessage {
event: "read" | "unread" | "archive" | "mark-all-read" | "opened" | "unpin";
type: "event";
messageId?: string;
error?: string;
}

export interface ITextElemental {
type: "text";
content: string;
}

export interface IActionElemental {
background_color?: string;
type: "action";
content: string;
href: string;
data?: Record<string, any>;
}

export interface IInboxMessagePreview {
type: "message";
archived?: string;
created: string;
messageId: string;
pinned?: {
slotId?: string;
};
actions?: Array<IActionElemental>;
preview?: string;
icon?: string;
opened?: string;
data?: Record<string, any>;
/** ISO 8601 date the message was read */
read?: string;
tags?: string[];
title?: string;
trackingIds?: {
openTrackingId?: string;
archiveTrackingId?: string;
clickTrackingId?: string;
deliverTrackingId?: string;
readTrackingId?: string;
unreadTrackingId?: string;
};
}

export interface ICourierEvent {
type?: "message" | "event";
data?: IInboxMessagePreview | ICourierEventMessage;
}

export type ICourierEventCallback = (event: ICourierEvent) => void;

export type Interceptor = (
message?: ICourierEventMessage | IInboxMessagePreview
) => IInboxMessagePreview | ICourierEventMessage | undefined;

import { ErrorEvent } from "reconnecting-websocket";
export type ErrorEventHandler = (event: ErrorEvent) => void;

export type PreferenceStatus = "OPTED_IN" | "OPTED_OUT" | "REQUIRED";

export type RepeatOn = {
sunday?: boolean;
monday?: boolean;
tuesday?: boolean;
wednesday?: boolean;
thursday?: boolean;
friday?: boolean;
saturday?: boolean;
};

export interface DigestSchedule {
period: string;
repetition: string;
scheduleId: string;
default?: boolean;
start: string;
recurrence: string;
repeat?: {
frequency: number;
interval: "day" | "week" | "month" | "year";
on?: string | RepeatOn;
};
end?: number | string;
}

export interface IPreferenceTemplate {
templateName: string;
templateId: string;
defaultStatus: PreferenceStatus;
digestSchedules?: DigestSchedule[];
}

export type WSOptions = {
url?: string;
onError?: ErrorEventHandler;
onClose?: () => void;
onReconnect?: () => void;
connectionTimeout?: number;
};

export interface PinDetails {
id: string;
label: {
value: string;
color: string;
};
icon: {
value: string;
color: string;
};
}
export interface Brand {
inapp?: {
disableCourierFooter?: boolean;
borderRadius?: string;
disableMessageIcon?: boolean;
placement?: "top" | "bottom" | "left" | "right";
emptyState?: {
textColor?: string;
text?: string;
};
widgetBackground?: {
topColor?: string;
bottomColor?: string;
};
icons?: {
bell?: string;
message?: string;
};
slots?: Array<PinDetails>;
toast?: {
borderRadius?: string;
timerAutoClose?: number;
};
renderActionsAsButtons?: boolean;
};
preferenceTemplates?: Array<IPreferenceTemplate>;
colors?: {
primary?: string;
secondary?: string;
tertiary?: string;
};
}
12 changes: 12 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"declarationDir": "./typings",
"paths": {
"~/*": ["./src/*"]
}
},
"include": ["../types/@types/*/index.d.ts", "./src/**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { renderHook } from "@testing-library/react-hooks"; // will attempt to auto-detect
import { Transport } from "./base";
import useTransport from "./use-transport";
import { CourierTransport, Transport } from "@trycourier/transport";
import useTransport from "../use-transport";

import { CourierWS } from "../ws";
jest.mock("@trycourier/transport");

const ws = new CourierWS({});
const courierTransportMock = CourierTransport as jest.Mock;
const courierTransport = new CourierTransport({
clientSourceId: "foo",
});

const mockConnect = ws.connect as jest.Mock;
const mockRenewSession = ws.renewSession as jest.Mock;
const renewSessionMock = courierTransport.renewSession as jest.Mock;

jest.mock("../ws");
describe("useTransport", () => {
afterEach(() => {
jest.clearAllMocks();
Expand All @@ -35,7 +36,7 @@ describe("useTransport", () => {
);

expect(result.current).toBeTruthy();
expect(mockConnect.mock.calls.length).toEqual(1);
expect(courierTransportMock.mock.calls.length).toEqual(1);
});

test("will call renewSession if a new token is provided", () => {
Expand All @@ -50,13 +51,13 @@ describe("useTransport", () => {
);

expect(result.current).toBeTruthy();
expect(mockConnect.mock.calls.length).toEqual(1);
expect(courierTransportMock.mock.calls.length).toEqual(1);

authorization =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InVzZXJfaWQ6NzBmNmE0ZjQtMjkwNy00NTE4LWI4ZjMtYjljZmFiMjI0NzY0IGluYm94OnJlYWQ6bWVzc2FnZXMiLCJ0ZW5hbnRfc2NvcGUiOiJwdWJsaXNoZWQvcHJvZHVjdGlvbiIsInRlbmFudF9pZCI6Ijc2ODI1MWNmLTNlYjgtNDI2YS05MmViLWZhYTBlNzY3ODc2OCIsImlhdCI6MTY3Mjc4MDE5MSwianRpIjoiMzU1NmU1OTYtNjljZi00NjdiLTg1YjMtNDk5ZjZmYzk2YjVhIn0.peUty0F94bhulmD4HS-7H7N3-HI31mIvU8jLFBEpUgM";

rerender();
expect(mockRenewSession.mock.calls.length).toEqual(1);
expect(renewSessionMock.mock.calls.length).toEqual(1);
});

test("will NOT call renewSession if a new token is provided but the scope changes", () => {
Expand All @@ -71,13 +72,13 @@ describe("useTransport", () => {
);

expect(result.current).toBeTruthy();
expect(mockConnect.mock.calls.length).toEqual(1);
expect(courierTransportMock.mock.calls.length).toEqual(1);

authorization =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InVzZXJfaWQ6YmxhaCBpbmJveDpyZWFkOm1lc3NhZ2VzIiwidGVuYW50X3Njb3BlIjoicHVibGlzaGVkL3Byb2R1Y3Rpb24iLCJ0ZW5hbnRfaWQiOiI3NjgyNTFjZi0zZWI4LTQyNmEtOTJlYi1mYWEwZTc2Nzg3NjgiLCJpYXQiOjE2NzI3ODIwNzYsImp0aSI6ImJjZjRiN2QzLWMyNDktNDQzNC04ZTQ0LWFjMTYxY2U0NTRiZCJ9.J7k0OQ1qfFR5MpdoP13mCusQWejpx7VB6Z6A194RxU8";

rerender();
expect(mockRenewSession.mock.calls.length).toEqual(0);
expect(mockConnect.mock.calls.length).toEqual(2);
expect(renewSessionMock.mock.calls.length).toEqual(0);
//expect(connectMock.mock.calls.length).toEqual(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useMemo, useRef } from "react";
import { CourierTransport, Transport, ITransportOptions } from "./index";
import {
CourierTransport,
Transport,
ITransportOptions,
} from "@trycourier/transport";
import jwtDecode from "jwt-decode";
interface DecodedAuth {
scope: string;
Expand Down
Loading

0 comments on commit 728461e

Please sign in to comment.