Skip to content
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
36 changes: 21 additions & 15 deletions src/js/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ function randomQuote(quote, crate) {
crate.notify(the_quote);
}

// Widgetbot initialization
loadScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function() {
let widgetbot = new Crate({
server: '804382334370578482',
channel: '804383092822900797',
defer: false,
});
function initDiscord() {
loadScript('https://cdn.jsdelivr.net/npm/@widgetbot/crate@3', function() {
let widgetbot = new Crate({
server: '804382334370578482',
channel: '804383092822900797',
defer: false,
});

// get random video game quotes and notify the user on Widgetbot after 7 minutes
fetchRandomQuote().then(quote => {
setTimeout(() => {
if (widgetbot) {
randomQuote(quote, widgetbot);
}
}, 7 * 60 * 1000);
// get random video game quotes and notify the user on Widgetbot after 7 minutes
fetchRandomQuote().then(quote => {
setTimeout(() => {
if (widgetbot) {
randomQuote(quote, widgetbot);
}
}, 7 * 60 * 1000);
});
});
});
}

module.exports = {
initDiscord,
randomQuote,
};
56 changes: 56 additions & 0 deletions tests/discord.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
describe,
expect,
it,
jest,
} from '@jest/globals';

const { initDiscord, randomQuote } = require('../src/js/discord');
const loadScript = require('../src/js/load-script');
const { fetchRandomQuote } = require('../src/js/random-quote');

jest.mock('../src/js/load-script');
jest.mock('../src/js/random-quote');

describe('initDiscord', () => {
it('should load the script and get a random quote', () => {
jest.useFakeTimers();
const mockCrate = {
notify: jest.fn()
};
global.Crate = jest.fn(() => mockCrate);

loadScript.mockImplementation((url, callback) => {
callback();
});

fetchRandomQuote.mockResolvedValue({
quote: 'Test quote',
quote_safe: 'Test quote safe'
});

initDiscord();

expect(global.Crate).toHaveBeenCalledWith({
server: '804382334370578482',
channel: '804383092822900797',
defer: false,
});

// advance timers
jest.advanceTimersByTime(7 * 60 * 1000);

expect(fetchRandomQuote).toHaveBeenCalled();
});
});

describe('randomQuote', () => {
it('should notify the user with the quote', () => {
const mockCrate = {
notify: jest.fn()
};

randomQuote({quote_safe: 'Test quote'}, mockCrate);
expect(mockCrate.notify).toHaveBeenCalledWith('Test quote');
});
});