# Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. #380 ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: #419 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
109 lines
No EOL
3.6 KiB
TypeScript
109 lines
No EOL
3.6 KiB
TypeScript
import { ButtonInteraction, TextChannel } from "discord.js";
|
|
import Claim from "../../src/buttonEvents/Claim";
|
|
import { ButtonInteraction as ButtonInteractionType } from "../__types__/discord.js";
|
|
import User from "../../src/database/entities/app/User";
|
|
import GenerateButtonInteractionMock from "../__functions__/discord.js/GenerateButtonInteractionMock";
|
|
|
|
jest.mock("../../src/client/appLogger");
|
|
|
|
let interaction: ButtonInteractionType;
|
|
|
|
beforeEach(() => {
|
|
jest.useFakeTimers();
|
|
jest.setSystemTime(1000 * 60 * 30);
|
|
|
|
interaction = GenerateButtonInteractionMock();
|
|
interaction.customId = "claim cardNumber claimId droppedBy userId";
|
|
});
|
|
|
|
afterAll(() => {
|
|
jest.useRealTimers();
|
|
});
|
|
|
|
test("GIVEN interaction.guild is null, EXPECT nothing to happen", async () => {
|
|
// Arrange
|
|
interaction.guild = null;
|
|
|
|
// Act
|
|
const claim = new Claim();
|
|
await claim.execute(interaction as unknown as ButtonInteraction);
|
|
|
|
// Assert
|
|
expect(interaction.deferUpdate).not.toHaveBeenCalled();
|
|
expect(interaction.editReply).not.toHaveBeenCalled();
|
|
expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("GIVEN interaction.guildId is null, EXPECT nothing to happen", async () => {
|
|
// Arrange
|
|
interaction.guildId = null;
|
|
|
|
// Act
|
|
const claim = new Claim();
|
|
await claim.execute(interaction as unknown as ButtonInteraction);
|
|
|
|
// Assert
|
|
expect(interaction.deferUpdate).not.toHaveBeenCalled();
|
|
expect(interaction.editReply).not.toHaveBeenCalled();
|
|
expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("GIVEN interaction.channel is null, EXPECT nothing to happen", async () => {
|
|
// Arrange
|
|
interaction.channel = null;
|
|
|
|
// Act
|
|
const claim = new Claim();
|
|
await claim.execute(interaction as unknown as ButtonInteraction);
|
|
|
|
// Assert
|
|
expect(interaction.deferUpdate).not.toHaveBeenCalled();
|
|
expect(interaction.editReply).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("GIVEN channel is not sendable, EXPECT nothing to happen", async () => {
|
|
// Arrange
|
|
interaction.channel!.isSendable = jest.fn().mockReturnValue(false);
|
|
|
|
// Act
|
|
const claim = new Claim();
|
|
await claim.execute(interaction as unknown as ButtonInteraction);
|
|
|
|
// Assert
|
|
expect(interaction.deferUpdate).not.toHaveBeenCalled();
|
|
expect(interaction.editReply).not.toHaveBeenCalled();
|
|
expect((interaction.channel as TextChannel).send).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("GIVEN interaction.message was created more than 5 minutes ago, EXPECT error", async () => {
|
|
// Arrange
|
|
interaction.message!.createdAt = new Date(0);
|
|
|
|
// Act
|
|
const claim = new Claim();
|
|
await claim.execute(interaction as unknown as ButtonInteraction);
|
|
|
|
// Assert
|
|
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
|
|
expect(interaction.channel!.send).toHaveBeenCalledWith("[object Object], Cards can only be claimed within 5 minutes of it being dropped!");
|
|
|
|
expect(interaction.editReply).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("GIVEN user.RemoveCurrency fails, EXPECT error", async () => {
|
|
// Arrange
|
|
User.FetchOneById = jest.fn().mockResolvedValue({
|
|
RemoveCurrency: jest.fn().mockReturnValue(false),
|
|
Currency: 5,
|
|
});
|
|
|
|
// Act
|
|
const claim = new Claim();
|
|
await claim.execute(interaction as unknown as ButtonInteraction);
|
|
|
|
// Assert
|
|
expect(interaction.channel!.send).toHaveBeenCalledTimes(1);
|
|
expect(interaction.channel!.send).toHaveBeenCalledWith("[object Object], Not enough currency! You need 10 currency, you have 5!");
|
|
|
|
expect(interaction.editReply).not.toHaveBeenCalled();
|
|
}); |