2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-03 05:10:47 +00:00
nativefier/src/options/fields/name.test.js
Ronan Jouchet 6fb3b92eb8
Upgrade dependencies and default to latest Electron 1.7.9 (PR #483)
* Update deps except eslint
* Update eslint and lint:fix (WIP, needs manual fixing for remaining 44 problems)
* Manually fix remaining eslint errors
* Document deprecation of `version-string` as of electron-packager 9.0.0
* Upgrade to Electron 1.7.9 (chrome-58, node-7.9.0, v8-5.8)
* npm: Disable generation of package-lock.json and gitignore it
  --Trying this, package-lock is a pain in PRs. May not be a good idea
  (obviously we lose deps pinning), will revert if necessary.--
* npm tasks: add dev-up-win for Windows developers,
  and e2e for end-to-end tests. Update docs.
* Move normalizeUrl test to a jest unit test, makes no sense to be in the mocha e2e tests
* Switch from babel-preset-es2015 to babel-preset-env,
  with target.node=4.0. Seem like it's today's most convenient
  way to support the latest ES and let babel transpile to what
  makes sense for our currently minimal node version
2017-11-14 08:05:01 -05:00

88 lines
2.9 KiB
JavaScript

import log from 'loglevel';
import name from './name';
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' };
test('it should not call inferTitle', () => name(params).then((result) => {
expect(inferTitle).toHaveBeenCalledTimes(0);
expect(result).toBe(params.name);
}));
test('it should call sanitize filename', () => name(params).then((result) => {
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', () => {
test('it should call inferTitle', () => name(params).then(() => {
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
}));
});
describe('when the name is an empty string', () => {
test('it should call inferTitle', () => {
const testParams = Object.assign({}, params, { name: '' });
return name(testParams).then(() => {
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
});
});
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' };
test('it should return the result from inferTitle', () => {
inferTitle.mockImplementationOnce(() => Promise.resolve(mockedResult));
return name(params).then((result) => {
expect(result).toBe(mockedResult);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
});
describe('when the returned pageTitle is falsey', () => {
test('it should return the default app name', () => {
inferTitle.mockImplementationOnce(() => Promise.resolve(null));
return name(params).then((result) => {
expect(result).toBe(DEFAULT_APP_NAME);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
});
});
});
describe('when inferTitle resolves with an error', () => {
test('it should return the default app name', () => {
inferTitle.mockImplementationOnce(() => Promise.reject(new Error('some error')));
return name(params).then((result) => {
expect(result).toBe(DEFAULT_APP_NAME);
expect(inferTitle).toHaveBeenCalledWith(params.targetUrl);
expect(log.warn).toHaveBeenCalledTimes(1);
});
});
});
});