Skip to content

Commit

Permalink
why are these test failing :(
Browse files Browse the repository at this point in the history
  • Loading branch information
nbramblett committed Aug 17, 2023
1 parent 93deee1 commit bc6ac3f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/chat-headless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"babel-jest": "^29.5.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-mock-extended": "^3.0.5",
"typescript": "^5.0.4"
}
}
46 changes: 26 additions & 20 deletions packages/chat-headless/tests/chatheadless.chatapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
RawResponse,
StreamResponse,
} from "@yext/chat-core";
import * as coreLib from "@yext/chat-core";
import { initialState } from "../src/slices/conversation";
import { Readable } from "stream";
import * as analyticsLib from "@yext/analytics";
import { mock as mockInterface } from "jest-mock-extended";

const config: ChatConfig = {
botId: "MY_BOT",
Expand All @@ -29,13 +31,18 @@ const mockedMetaState: MetaState = {
},
};

let coreMock = mockInterface<ChatCore>();

jest.mock("@yext/analytics");
jest.mock("@yext/chat-core");

beforeEach(() => {
sessionStorage.clear();
jest.spyOn(analyticsLib, "provideChatAnalytics").mockReturnValue({
report: jest.fn(),
});
coreMock = mockInterface<ChatCore>();
jest.spyOn(coreLib, "provideChatCore").mockReturnValue(coreMock);
});

describe("Chat API methods work as expected", () => {
Expand Down Expand Up @@ -103,10 +110,9 @@ describe("Chat API methods work as expected", () => {
}

it("getNextMessage works as expected", async () => {
const coreGetNextMessageSpy =
coreMock.getNextMessage.mockResolvedValueOnce(expectedResponse);
const chatHeadless = provideChatHeadless(config);
const coreGetNextMessageSpy = jest
.spyOn(ChatCore.prototype, "getNextMessage")
.mockResolvedValueOnce(expectedResponse);
await testAPI(
chatHeadless,
chatHeadless.getNextMessage,
Expand All @@ -116,13 +122,15 @@ describe("Chat API methods work as expected", () => {

it("streamNextMessage works as expected", async () => {
const chatHeadless = provideChatHeadless(config);
const coreStreamtNextMessageSpy = jest
.spyOn(ChatCore.prototype, "streamNextMessage")
.mockResolvedValueOnce(
const coreStreamNextMessageSpy =
coreMock.streamNextMessage.mockResolvedValueOnce(
new StreamResponse({
ok: true,
body: new Readable({
read() {
console.log(
"For some reason this doesn't ever get run, is it mocking the StreamResponse somehow?"
);
this.push(
'event: startTokenStream\ndata: { "currentGoal": "SOME_GOAL" }\n\n'
);
Expand All @@ -144,7 +152,7 @@ describe("Chat API methods work as expected", () => {
await testAPI(
chatHeadless,
chatHeadless.streamNextMessage,
coreStreamtNextMessageSpy
coreStreamNextMessageSpy
);
// 1 for user's message, 3 for each streamToken event, and 1 for endStream event
expect(setMessagesSpy).toBeCalledTimes(5);
Expand Down Expand Up @@ -183,9 +191,8 @@ describe("Chat API methods work as expected", () => {

it("logs error when streamNextMessage failed to get full message response at end of stream", async () => {
const chatHeadless = provideChatHeadless(config);
const coreStreamNextMessageSpy = jest
.spyOn(ChatCore.prototype, "streamNextMessage")
.mockResolvedValueOnce(
const coreStreamNextMessageSpy =
coreMock.streamNextMessage.mockResolvedValueOnce(
new StreamResponse({
ok: true,
body: new Readable({
Expand All @@ -201,7 +208,8 @@ describe("Chat API methods work as expected", () => {
}),
} as unknown as RawResponse)
);
expect.assertions(2);
expect.assertions(3);
const setMessagesSpy = jest.spyOn(chatHeadless, "setMessages");

try {
await chatHeadless.streamNextMessage("This is a dummy text!");
Expand All @@ -212,13 +220,13 @@ describe("Chat API methods work as expected", () => {
);
}
expect(coreStreamNextMessageSpy).toBeCalledTimes(1);
expect(setMessagesSpy).toBeCalledTimes(4);
});

it("logs warning when attempt to send next message to API when it is still processing", async () => {
const chatHeadless = provideChatHeadless(config);
const coreGetNextMessageSpy = jest
.spyOn(ChatCore.prototype, "getNextMessage")
.mockResolvedValueOnce(expectedResponse);
const coreGetNextMessageSpy =
coreMock.getNextMessage.mockResolvedValueOnce(expectedResponse);
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
chatHeadless.getNextMessage("message 1");
const secondResponse = await chatHeadless.getNextMessage("message 2");
Expand All @@ -243,9 +251,8 @@ describe("Chat API methods work as expected", () => {
const errorMessage =
"Chat API error: FATAL_ERROR: Invalid API Key. (code: 1)";
const chatHeadless = provideChatHeadless(config);
const coreGetNextMessageSpy = jest
.spyOn(ChatCore.prototype, "getNextMessage")
.mockRejectedValue(errorMessage);
const coreGetNextMessageSpy =
coreMock.getNextMessage.mockRejectedValue(errorMessage);
expect.assertions(3);

try {
Expand All @@ -271,9 +278,8 @@ describe("Chat API methods work as expected", () => {
const chatHeadless = provideChatHeadless(config);
expect(chatHeadless.state.conversation.messages).toEqual([]);

const coreGetNextMessageSpy = jest
.spyOn(ChatCore.prototype, "getNextMessage")
.mockResolvedValueOnce(expectedResponse);
const coreGetNextMessageSpy =
coreMock.getNextMessage.mockResolvedValueOnce(expectedResponse);
await chatHeadless.getNextMessage();
expect(coreGetNextMessageSpy).toBeCalledTimes(1);
expect(coreGetNextMessageSpy).toBeCalledWith({
Expand Down

0 comments on commit bc6ac3f

Please sign in to comment.