Skip to content

Commit

Permalink
test: more (#1987)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and hiroppy committed Jun 6, 2019
1 parent 176283b commit df25433
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/utils/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const log = require('webpack-log');

function createLogger(options) {
function createLogger(options = {}) {
let level = options.logLevel || 'info';

if (options.noInfo === true) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/runOpen.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function runOpen(uri, options, log) {
openMessage += `: ${options.open}`;
}

open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
return open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
log.warn(
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
);
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/tryParseInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

function tryParseInt(input) {
const output = parseInt(input, 10);

if (Number.isNaN(output)) {
return null;
}

return output;
}

Expand Down
2 changes: 1 addition & 1 deletion test/server/utils/__snapshots__/findPort.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`findPort cli utility function should throw the error when the port isn't found 1`] = `"No open ports found in between 8080 and 8085"`;
exports[`findPort util should throws the error when the port isn't found 1`] = `"No open ports found in between 8080 and 8085"`;
47 changes: 47 additions & 0 deletions test/server/utils/createLogger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const createLogger = require('../../../lib/utils/createLogger');

describe('createLogger util', () => {
it('should create logger without options', () => {
const logger = createLogger();

expect(logger.name).toBe('wds');
expect(logger.currentLevel).toBe(2);
});

it('should create logger with logLevel option (debug)', () => {
const logger = createLogger({ logLevel: 'debug' });

expect(logger.name).toBe('wds');
expect(logger.currentLevel).toBe(1);
});

it('should create logger with logLevel option (warn)', () => {
const logger = createLogger({ logLevel: 'warn' });

expect(logger.name).toBe('wds');
expect(logger.currentLevel).toBe(3);
});

it('should create logger with noInfo option', () => {
const logger = createLogger({ noInfo: true });

expect(logger.name).toBe('wds');
expect(logger.currentLevel).toBe(3);
});

it('should create logger with quiet option', () => {
const logger = createLogger({ quiet: true });

expect(logger.name).toBe('wds');
expect(logger.currentLevel).toBe(5);
});

it('should create logger with logTime option', () => {
const logger = createLogger({ logTime: true });

expect(logger.name).toBe('wds');
expect(logger.currentLevel).toBe(2);
});
});
9 changes: 9 additions & 0 deletions test/server/utils/defaultPort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const defaultPort = require('../../../lib/utils/defaultPort');

describe('defaultPort util', () => {
it('should return value', () => {
expect(defaultPort).toBe(8080);
});
});
20 changes: 20 additions & 0 deletions test/server/utils/defaultTo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const defaultTo = require('../../../lib/utils/defaultTo');

const noop = () => {};
const array = [1, 2, 3];

describe('defaultTo util', () => {
it('should returns value', () => {
expect(defaultTo(0, 200)).toBe(0);
expect(defaultTo(100, 200)).toBe(100);
expect(defaultTo('', 200)).toBe('');
expect(defaultTo('string', 200)).toBe('string');
expect(defaultTo(noop, 200)).toBe(noop);
expect(defaultTo(array, 200)).toEqual(array);
expect(defaultTo(null, 200)).toBe(200);
// eslint-disable-next-line no-undefined
expect(defaultTo(undefined, 200)).toBe(200);
});
});
6 changes: 3 additions & 3 deletions test/server/utils/findPort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const http = require('http');
const findPort = require('../../../lib/utils/findPort');

describe('findPort cli utility function', () => {
describe('findPort util', () => {
let dummyServers = [];

afterEach(() => {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('findPort cli utility function', () => {
}, Promise.resolve());
}

it('should return the port when the port is specified', () => {
it('should returns the port when the port is specified', () => {
process.env.DEFAULT_PORT_RETRY = 5;

return findPort(8082).then((port) => {
Expand All @@ -54,7 +54,7 @@ describe('findPort cli utility function', () => {
});
});

it("should throw the error when the port isn't found", () => {
it("should throws the error when the port isn't found", () => {
process.env.DEFAULT_PORT_RETRY = 5;

return createDummyServers(10)
Expand Down
8 changes: 4 additions & 4 deletions test/server/utils/getSocketServerImplementation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const getSocketServerImplementation = require('../../../lib/utils/getSocketServe
const SockJSServer = require('../../../lib/servers/SockJSServer');

describe('getSocketServerImplementation util', () => {
it("should work with serverMode: 'sockjs'", () => {
it("should works with string serverMode ('sockjs')", () => {
let result;

expect(() => {
Expand All @@ -16,7 +16,7 @@ describe('getSocketServerImplementation util', () => {
expect(result).toEqual(SockJSServer);
});

it('should work with serverMode: SockJSServer class', () => {
it('should works with serverMode (SockJSServer class)', () => {
let result;

expect(() => {
Expand All @@ -28,7 +28,7 @@ describe('getSocketServerImplementation util', () => {
expect(result).toEqual(SockJSServer);
});

it('should work with serverMode: SockJSServer full path', () => {
it('should work with serverMode (SockJSServer full path)', () => {
let result;

expect(() => {
Expand All @@ -40,7 +40,7 @@ describe('getSocketServerImplementation util', () => {
expect(result).toEqual(SockJSServer);
});

it('should throw with serverMode: bad path', () => {
it('should throws with serverMode (bad path)', () => {
expect(() => {
getSocketServerImplementation({
serverMode: '/bad/path/to/implementation',
Expand Down
134 changes: 134 additions & 0 deletions test/server/utils/runOpen.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
'use strict';

const opn = require('opn');
const runOpen = require('../../../lib/utils/runOpen');

jest.mock('opn');

describe('runOpen util', () => {
afterEach(() => {
opn.mockClear();
});

describe('should open browser', () => {
beforeEach(() => {
opn.mockImplementation(() => Promise.resolve());
});

it('on specify URL', () => {
return runOpen('https://example.com', {}, console).then(() => {
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
});
});

it('on specify URL with page', () => {
return runOpen(
'https://example.com',
{ openPage: '/index.html' },
console
).then(() => {
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{},
]);
});
});

it('on specify URL in Google Chrome', () => {
return runOpen(
'https://example.com',
{ open: 'Google Chrome' },
console
).then(() => {
expect(opn.mock.calls[0]).toEqual([
'https://example.com',
{ app: 'Google Chrome' },
]);
});
});

it('on specify URL with page in Google Chrome ', () => {
return runOpen(
'https://example.com',
{ open: 'Google Chrome', openPage: '/index.html' },
console
).then(() => {
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{ app: 'Google Chrome' },
]);
});
});
});

describe('should not open browser', () => {
const logMock = { warn: jest.fn() };

beforeEach(() => {
opn.mockImplementation(() => Promise.reject());
});

afterEach(() => {
logMock.warn.mockClear();
});

it('on specify URL and log error', () => {
return runOpen('https://example.com', {}, logMock).then(() => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
});
});

it('on specify URL with page and log error', () => {
return runOpen(
'https://example.com',
{ openPage: '/index.html' },
logMock
).then(() => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{},
]);
});
});

it('on specify URL in Google Chrome and log error', () => {
return runOpen(
'https://example.com',
{ open: 'Google Chrome' },
logMock
).then(() => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual([
'https://example.com',
{
app: 'Google Chrome',
},
]);
});
});

it('on specify URL with page in Google Chrome and log error ', () => {
return runOpen(
'https://example.com',
{ open: 'Google Chrome', openPage: '/index.html' },
logMock
).then(() => {
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
);
expect(opn.mock.calls[0]).toEqual([
'https://example.com/index.html',
{ app: 'Google Chrome' },
]);
});
});
});
});
22 changes: 22 additions & 0 deletions test/server/utils/tryParseInt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const tryParseInt = require('../../../lib/utils/tryParseInt');

describe('tryParseInt util', () => {
it('should parser number as number', () => {
expect(tryParseInt(1)).toBe(1);
});

it('should parser string as number', () => {
expect(tryParseInt('1')).toBe(1);
});

it('should parser undefined as null', () => {
// eslint-disable-next-line no-undefined
expect(tryParseInt(undefined)).toBe(null);
});

it('should parser NaN as null', () => {
expect(tryParseInt(NaN)).toBe(null);
});
});

0 comments on commit df25433

Please sign in to comment.