Skip to content

Commit

Permalink
test: add tests for and operator in some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgmagalhaes committed May 15, 2023
1 parent c654480 commit 636da55
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 42 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion e2e/cases.ts
Expand Up @@ -119,7 +119,7 @@ export const testCases: ITestFile[] = [
},
{
id: 19,
folder: "toHaveResult",
folder: "and",
testFile: "test1.spec.ts",
exitCodeExpectation: 0,
},
Expand Down
8 changes: 4 additions & 4 deletions e2e/pipeline.ts
Expand Up @@ -64,9 +64,7 @@ async function main() {
if (selectedTests.length === 0 || selectedTests.includes(fileObj.id.toString())) {
timer.start();
const output = await testFn();
//console.log(chalk.cyanBright(`Output of: ${fileObj.testFile}\n`));

//testUtils.saveOutput(fileObj.testFile, output);
timer.stop();

if (testFailed(output, fileObj)) {
_testsPass = false;
Expand All @@ -79,7 +77,9 @@ async function main() {
);
return 1;
} else {
process.stdout.write(`${chalk.green(" OK")}\n`);
process.stdout.write(
`${chalk.green(" OK")} in ${chalk.yellow(timer.elapsedFormatted)}\n`,
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/index.ts
Expand Up @@ -309,7 +309,7 @@ type DebugTypes<T> = {
testName: string;
message: string;
trace?: string;
}>
}> & { and: DebugTypes<T> }
: T[P] extends (...params: any[]) => CommandState
? (...params: Parameters<T[P]>) => DebugTypes<T>
: T[P] extends Record<string, any>
Expand Down
16 changes: 16 additions & 0 deletions tests/command/message/__snapshots__/addReaction.test.ts.snap
Expand Up @@ -62,6 +62,15 @@ received: 😀",
}
`;

exports[`testing addReaction function should return a failed test with isNot = false using 'and' keyword and 'not' keyword 1`] = `
Object {
"message": "expected: not to add reactions 😀
received: 😀",
"pass": false,
"testName": "addReaction",
}
`;

exports[`testing addReaction function should return a failed test with isNot = true 1`] = `
Object {
"message": "expected: not to add reactions 😀
Expand All @@ -85,6 +94,13 @@ Object {
}
`;

exports[`testing addReaction function should return a passed test with isNot = false using 'and' keyword 1`] = `
Object {
"pass": true,
"testName": "addReaction",
}
`;

exports[`testing addReaction function should return a passed test with isNot = true 1`] = `
Object {
"pass": true,
Expand Down
7 changes: 7 additions & 0 deletions tests/command/message/__snapshots__/matchEmbed.test.ts.snap
Expand Up @@ -16,6 +16,13 @@ Object {
}
`;

exports[`testing matchEmbed function should get success test due to bot returned equal messages that matches with 'and' keyword 1`] = `
Object {
"pass": true,
"testName": "matchEmbed",
}
`;

exports[`testing matchEmbed function should return a failed test due to invalid parameter (null) 1`] = `
Object {
"message": "expected: parameter to be an object of type IMessageEmbed
Expand Down
Expand Up @@ -31,6 +31,13 @@ Object {
}
`;

exports[`testing messageContentContains function should get success test due to bot returned equal message with 'and' keyword 1`] = `
Object {
"pass": true,
"testName": "messageContentContains",
}
`;

exports[`testing messageContentContains function should get success test due to timeout but isNot = true 1`] = `
Object {
"pass": true,
Expand Down
16 changes: 16 additions & 0 deletions tests/command/message/__snapshots__/pinMessage.test.ts.snap
Expand Up @@ -35,6 +35,15 @@ received: message pin = false",
}
`;

exports[`testing pinMessage function should return a failed test due to message pinned with 'and' keyword using 'not' 1`] = `
Object {
"message": "expected: to not pin message of id 1233
received: message pin = false",
"pass": false,
"testName": "pinMessage",
}
`;

exports[`testing pinMessage function should return a passed test due to isNot true and timeout 1`] = `
Object {
"pass": true,
Expand All @@ -49,6 +58,13 @@ Object {
}
`;

exports[`testing pinMessage function should return a passed test due to message pinned with 'and' keyword 1`] = `
Object {
"pass": true,
"testName": "pinMessage",
}
`;

exports[`testing pinMessage function should return error message due to no messageIdentifier (null) 1`] = `
Object {
"message": "expected: message identifier to be a string or a IMessageIdentifier object
Expand Down
30 changes: 30 additions & 0 deletions tests/command/message/addReaction.test.ts
Expand Up @@ -68,6 +68,36 @@ describe(`testing ${testName} function`, () => {
expect(report).toMatchSnapshot();
});

it("should return a passed test with isNot = false using 'and' keyword ", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessageReactionsAdd();

cordeClient.sendTextMessage = jest.fn().mockReturnValue(mockDiscord.message);

const report = await debugCon()
.should.addReaction([mockDiscord.messageReaction.emoji.name])
.and.addReaction([mockDiscord.messageReaction.emoji.name]);

expect(report).toEqual(passReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should return a failed test with isNot = false using 'and' keyword and 'not' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessageReactionsAdd();

cordeClient.sendTextMessage = jest.fn().mockReturnValue(mockDiscord.message);

const report = await debugCon()
.should.addReaction([mockDiscord.messageReaction.emoji.name])
.and.not.addReaction([mockDiscord.messageReaction.emoji.name]);

expect(report).toMatchObject(failReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should return a passed test with isNot = true", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
events.mockOnceMessageReactionsAddToReject();
Expand Down
26 changes: 24 additions & 2 deletions tests/command/message/editMessage.test.ts
Expand Up @@ -73,15 +73,37 @@ describe(`testing ${testName} function`, () => {
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned equal message", async () => {
it("should get success test due to timeout but isNot = true with", async () => {
const report = await debugCon().should.not.editMessage("pong");
expect(report).toEqual(passReport);
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned equal message 'and' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
events.mockOnceMessageContentOrEmbedChange();

const report = await debugCon().should.editMessage(mockDiscord.message.content);
const report = await debugCon()
.should.editMessage(mockDiscord.message.content)
.and.editMessage(mockDiscord.message.content);

expect(report).toEqual(passReport);
expect(report).toMatchSnapshot();
});

it("should get invalid test due to bot returned equal message 'and' keyword and 'not' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessageContentOrEmbedChange();

const report = await debugCon()
.should.editMessage(mockDiscord.message.content)
.and.not.editMessage(mockDiscord.message.content);

expect(report).toMatchObject(failReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned equal messages (string type)", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
events.mockOnceMessageContentOrEmbedChange();
Expand Down
32 changes: 31 additions & 1 deletion tests/command/message/matchEmbed.test.ts
Expand Up @@ -27,7 +27,7 @@ function debugCon(customCommand?: string, customChannelId?: string, customClient
function mockEmbedMessage() {
const mockEvents = new MockEvents(cordeClient, mockDiscord);
mockDiscord.message.embeds.push(mockDiscord.messageEmbed);
mockEvents.mockOnceMessage(mockDiscord.message);
return mockEvents.mockOnceMessage(mockDiscord.message);
}

describe(`testing ${testName} function`, () => {
Expand All @@ -53,6 +53,36 @@ describe(`testing ${testName} function`, () => {
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned equal messages that matches with 'and' keyword", async () => {
const mock = mockEmbedMessage();
const report = await debugCon()
.should.matchEmbed({
author: mockDiscord.messageEmbed.author,
})
.and.matchEmbed({
author: mockDiscord.messageEmbed.author,
});

expect(report).toEqual(passReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should get failed test due to bot returned equal messages that matches with 'and' and 'not' keywords", async () => {
const mock = mockEmbedMessage();
const report = await debugCon()
.should.matchEmbed({
author: mockDiscord.messageEmbed.author,
})
.and.not.matchEmbed({
author: mockDiscord.messageEmbed.author,
});

expect(report).toMatchObject(failReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should get failed test due to bot returned equal messages that matches isNot(true)", async () => {
mockEmbedMessage();
const report = await debugCon().should.not.matchEmbed({
Expand Down
26 changes: 26 additions & 0 deletions tests/command/message/messageContentContains.test.ts
Expand Up @@ -67,6 +67,32 @@ describe(`testing ${testName} function`, () => {
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned equal message with 'and' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessage();

const report = await debugCon()
.should.messageContentContains(mockDiscord.message.content)
.and.messageContentContains(mockDiscord.message.content);

expect(report).toEqual(passReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned equal message with 'and' and 'not' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessage();

const report = await debugCon()
.should.messageContentContains(mockDiscord.message.content)
.and.not.messageContentContains(mockDiscord.message.content);

expect(report).toMatchObject(failReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should get success test due to bot returned a contained message", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
mockDiscord.message.content = "message to contain";
Expand Down
42 changes: 30 additions & 12 deletions tests/command/message/pinMessage.test.ts
Expand Up @@ -29,33 +29,25 @@ describe(`testing ${testName} function`, () => {
});

it("should return error message due to no messageIdentifier (null)", async () => {
const report = await debugCon()
// @ts-expect-error
.should.pinMessage(null);
const report = await debugCon().should.pinMessage(null);
expect(report).toMatchObject(failReport);
expect(report).toMatchSnapshot();
});

it("should return error message due to no messageIdentifier (undefined)", async () => {
const report = await debugCon()
// @ts-expect-error
.should.pinMessage(undefined);
const report = await debugCon().should.pinMessage(undefined);
expect(report).toMatchObject(failReport);
expect(report).toMatchSnapshot();
});

it("should return a passed test due to isNot true and timeout", async () => {
const report = await debugCon()
.should.not // @ts-expect-error
.pinMessage("");
const report = await debugCon().should.not.pinMessage("");
expect(report).toEqual(passReport);
expect(report).toMatchSnapshot();
});

it("should return a failed test due to isNot false and timeout", async () => {
const report = await debugCon()
.should // @ts-expect-error
.pinMessage("1233");
const report = await debugCon().should.pinMessage("1233");
expect(report).toMatchObject(failReport);
expect(report).toMatchSnapshot();
});
Expand All @@ -76,6 +68,32 @@ describe(`testing ${testName} function`, () => {
expect(report).toMatchSnapshot();
});

it("should return a passed test due to message pinned with 'and' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessagePinned(mockDiscord.pinnedMessage);

const report = await debugCon()
.should.pinMessage({ id: "1233" })
.and.pinMessage({ id: "1233" });

expect(report).toEqual(passReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should return a failed test due to message pinned with 'and' keyword using 'not'", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessagePinned(mockDiscord.pinnedMessage);

const report = await debugCon()
.should.pinMessage({ id: "1233" })
.and.not.pinMessage({ id: "1233" });

expect(report).toMatchObject(failReport);
expect(mock).toBeCalledTimes(2);
expect(report).toMatchSnapshot();
});

it("should return a failed test due to message pinned but isNot true", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
events.mockOnceMessagePinned(mockDiscord.pinnedMessage);
Expand Down
12 changes: 12 additions & 0 deletions tests/command/message/respond.test.ts
Expand Up @@ -65,6 +65,18 @@ describe(`testing ${testName} function`, () => {
expect(report).toMatchObject(passReport);
});

it("should get success test due to bot returned equal message using 'and' keyword", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
const mock = events.mockOnceMessageCreate();

const report = await debugCon()
.should.respond(mockDiscord.message.content)
.and.respond(mockDiscord.message.content);

expect(report).toMatchObject(passReport);
expect(mock).toBeCalledTimes(2);
});

it("should get success test due to bot returned equal messages (string type)", async () => {
const events = new MockEvents(cordeClient, mockDiscord);
events.mockOnceMessageCreate();
Expand Down

0 comments on commit 636da55

Please sign in to comment.