Nativefier/src/options/fields/name.test.js

94 lines
2.9 KiB
JavaScript
Raw Normal View History

import log from 'loglevel';
import name from './name';
2018-07-21 15:16:02 +02:00
import { DEFAULT_APP_NAME } from '../../constants';
import { inferTitle } from '../../infer';
import { sanitizeFilename } from '../../utils';
jest.mock('./../../infer/inferTitle');
jest.mock('./../../utils/sanitizeFilename');
jest.mock('loglevel');
sanitizeFilename.mockImplementation((_, filename) => filename);
const mockedResult = 'mock name';
describe('well formed name parameters', () => {
const params = { name: 'appname', platform: 'something' };
2018-05-25 07:24:09 +02:00
test('it should not call inferTitle', async () => {
const result = await name(params);
2018-05-24 09:02:44 +02:00
2018-05-25 07:24:09 +02:00
expect(inferTitle).toHaveBeenCalledTimes(0);
expect(result).toBe(params.name);
});
test('it should call sanitize filename', async () => {
const result = await name(params);
expect(sanitizeFilename).toHaveBeenCalledWith(params.platform, result);
});
});
describe('bad name parameters', () => {
beforeEach(() => {
inferTitle.mockImplementationOnce(() => Promise.resolve(mockedResult));
});
const params = { targetUrl: 'some url' };
describe('when the name is undefined', () => {
2018-05-25 07:24:09 +02:00
test('it should call inferTitle', async () => {
await name(params);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
});
describe('when the name is an empty string', () => {
2018-05-25 07:24:09 +02:00
test('it should call inferTitle', async () => {
const testParams = {
...params,
name: '',
};
2018-05-25 07:24:09 +02:00
await name(testParams);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
});
2018-05-24 09:02:44 +02:00
test('it should call sanitize filename', () =>
name(params).then((result) => {
expect(sanitizeFilename).toHaveBeenCalledWith(params.platform, result);
}));
});
describe('handling inferTitle results', () => {
const params = { targetUrl: 'some url', name: '', platform: 'something' };
2018-05-25 07:24:09 +02:00
test('it should return the result from inferTitle', async () => {
inferTitle.mockImplementationOnce(() => Promise.resolve(mockedResult));
2018-05-25 07:24:09 +02:00
const result = await name(params);
expect(result).toBe(mockedResult);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
describe('when the returned pageTitle is falsey', () => {
2018-05-25 07:24:09 +02:00
test('it should return the default app name', async () => {
inferTitle.mockImplementationOnce(() => Promise.resolve(null));
2018-05-25 07:24:09 +02:00
const result = await name(params);
expect(result).toBe(DEFAULT_APP_NAME);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
});
describe('when inferTitle resolves with an error', () => {
2018-05-25 07:24:09 +02:00
test('it should return the default app name', async () => {
2018-05-24 09:02:44 +02:00
inferTitle.mockImplementationOnce(() =>
Promise.reject(new Error('some error')),
);
2018-05-25 07:24:09 +02:00
const result = await name(params);
expect(result).toBe(DEFAULT_APP_NAME);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
expect(log.warn).toHaveBeenCalledTimes(1);
});
});
});