|
| 1 | +import Search from '../../src/commands/search'; |
| 2 | +import config from '../../src/config'; |
| 3 | +import axiosMock from '../__mocks__/axios'; |
| 4 | +import { message as mockMessage, MockedMessage } from '../mocks/discord'; |
| 5 | +import { noResults, results } from './../mockData/search'; |
| 6 | + |
| 7 | +jest.mock('axios'); |
| 8 | +jest.mock('../../src/config.ts'); |
| 9 | + |
| 10 | +let sendMock: MockedMessage; |
| 11 | +beforeEach(() => { |
| 12 | + sendMock = jest.fn(); |
| 13 | + mockMessage.channel.send = sendMock; |
| 14 | + mockMessage.reply = sendMock; |
| 15 | + config.googleApiKey = 'abc123'; |
| 16 | + config.googleSearchEngineId = '12345678910:some-thing'; |
| 17 | +}); |
| 18 | + |
| 19 | +const setupRequiredMessage = 'Setup Required: Configure Google API keys in the environment variables'; |
| 20 | + |
| 21 | +describe('Search Command', () => { |
| 22 | + describe('Environment Variables', () => { |
| 23 | + test('Setup message when missing Google API Key ENV Var', async () => { |
| 24 | + config.googleApiKey = undefined; |
| 25 | + await Search.execute(['Missing Google API Key'], mockMessage); |
| 26 | + expect(sendMock).lastCalledWith(setupRequiredMessage); |
| 27 | + }); |
| 28 | + test('Setup message when missing Google Search Engine ID ENV Var', async () => { |
| 29 | + config.googleSearchEngineId = undefined; |
| 30 | + await Search.execute(['Missing Google Search Engine ID'], mockMessage); |
| 31 | + expect(sendMock).lastCalledWith(setupRequiredMessage); |
| 32 | + }); |
| 33 | + }); |
| 34 | + test('With no results', async () => { |
| 35 | + const mockedData = Promise.resolve({ data: noResults }); |
| 36 | + axiosMock.get.mockResolvedValueOnce(mockedData); |
| 37 | + await Search.execute(['Nothing here'], mockMessage); |
| 38 | + expect(sendMock).lastCalledWith('`No results found.`'); |
| 39 | + }); |
| 40 | + test('With results', async () => { |
| 41 | + const mockedData = Promise.resolve({ data: results }); |
| 42 | + axiosMock.get.mockResolvedValueOnce(mockedData); |
| 43 | + await Search.execute(['dingusy'], mockMessage); |
| 44 | + expect(sendMock).lastCalledWith(results.items[0].link); |
| 45 | + }); |
| 46 | + test('Malformed Response', async () => { |
| 47 | + const mockedData = Promise.resolve({ data: {} }); |
| 48 | + axiosMock.get.mockResolvedValueOnce(mockedData); |
| 49 | + await Search.execute(['NOPE'], mockMessage); |
| 50 | + expect(sendMock).lastCalledWith("I'm Sorry Dave, I'm afraid I can't do that..."); |
| 51 | + }); |
| 52 | +}); |
0 commit comments