Skip to content

Commit

Permalink
[test] add unit regarding the error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zlargon committed Nov 8, 2020
1 parent e670f66 commit 8555814
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/error-handle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const fs = require('fs');
const tts = require('..');
const fetch = require('isomorphic-fetch');
jest.setTimeout(60000);

jest.mock('isomorphic-fetch');
const host = 'https://translate.google.com';

const html = {
hasTokenKey: fs.readFileSync(`${__dirname}/__mocks__/has-tkk.html`, 'utf8'),
noTokenKey: fs.readFileSync(`${__dirname}/__mocks__/no-tkk.html`, 'utf8'),
};

const mockResponse = {
htmlFailed: () => {
fetch.mockImplementationOnce(async (url) => {
if (url !== host) throw new Error('test failed');
return { status: 400, statusText: 'request failed' };
});
},
htmlSuccessWithTokenKey: () => {
fetch.mockImplementationOnce(async (url) => {
if (url !== host) throw new Error('test failed');
return { status: 200, text: async () => html.hasTokenKey };
});
},
htmlSuccessButNoTokenKey: () => {
fetch.mockImplementationOnce(async (url) => {
if (url !== host) throw new Error('text failed');
return { status: 200, text: async () => html.noTokenKey };
});
},
validateSucceed: () => {
fetch.mockImplementationOnce(async (url) => {
if (url === host) throw new Error('test failed');
return { status: 200 };
});
},
validateFailed: () => {
fetch.mockImplementationOnce(async (url) => {
if (url === host) throw new Error('test failed');
return { status: 400 };
});
},
};

describe('Google TTS', () => {
beforeEach(fetch.mockReset);

describe('first time using tts function', () => {
it('get html failed', async () => {
mockResponse.htmlFailed();
await expect(tts('hello')).rejects.toThrow('status code = 400');
expect(fetch).toHaveBeenCalledTimes(1);
});

it('cannot get the token key (retry 10 times)', async () => {
for (let i = 0; i < 10; i++) {
mockResponse.htmlSuccessButNoTokenKey();
}
await expect(tts('hello')).rejects.toThrow('get token key failed from google');
expect(fetch).toHaveBeenCalledTimes(10);
});

it('retry 3 times to get the token key', async () => {
mockResponse.htmlSuccessButNoTokenKey();
mockResponse.htmlSuccessButNoTokenKey();
mockResponse.htmlSuccessButNoTokenKey();
mockResponse.htmlSuccessWithTokenKey();

const url = await tts('hello');
expect(url).toBe(
'https://translate.google.com/translate_tts?ie=UTF-8&q=hello&tl=en&total=1&idx=0&textlen=5&tk=918197.577527&client=t&prev=input&ttsspeed=1'
);
expect(fetch).toHaveBeenCalledTimes(4);
});
});

describe('non-first time using tts function', () => {
it('validate success', async () => {
mockResponse.validateSucceed();
const url = await tts('test', 'en', 0.24);

expect(url).toBe(
'https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=en&total=1&idx=0&textlen=4&tk=376585.225867&client=t&prev=input&ttsspeed=0.24'
);
expect(fetch).toHaveBeenCalledTimes(1);
});

it('validate failed: retry 3 times to get the new token', async () => {
mockResponse.validateFailed();
mockResponse.htmlSuccessButNoTokenKey();
mockResponse.htmlSuccessButNoTokenKey();
mockResponse.htmlSuccessButNoTokenKey();
fetch.mockImplementationOnce(async (url) => {
if (url !== host) throw new Error('test failed');
return { status: 200, text: async () => "......tkk:'1234557.1234567'....." };
});

const url = await tts('test');
expect(url).toBe(
'https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=en&total=1&idx=0&textlen=4&tk=321164.1454321&client=t&prev=input&ttsspeed=1'
);
expect(fetch).toHaveBeenCalledTimes(5);
});
});
});

0 comments on commit 8555814

Please sign in to comment.