Skip to content

Commit

Permalink
test(experimental-utils): add few missing tests (#1449)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed Jan 14, 2020
1 parent 4726605 commit 8dcdfb1
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 8 deletions.
46 changes: 46 additions & 0 deletions packages/experimental-utils/tests/eslint-utils/RuleCreator.test.ts
@@ -0,0 +1,46 @@
import { ESLintUtils } from '../../src';

describe('RuleCreator', () => {
const createRule = ESLintUtils.RuleCreator(name => `test/${name}`);

it('createRule should be a function', () => {
expect(typeof createRule).toEqual('function');
});

it('should create rule correctly', () => {
const rule = createRule({
name: 'test',
meta: {
docs: {
description: 'some description',
category: 'Best Practices',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
foo: 'some message',
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create() {
return {};
},
});
expect(rule.meta).toEqual({
docs: {
description: 'some description',
category: 'Best Practices',
url: 'test/test',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
foo: 'some message',
},
schema: [],
type: 'problem',
});
});
});
@@ -1,10 +1,10 @@
import * as util from '../../src/eslint-utils/applyDefault';
import { ESLintUtils } from '../../src';

describe('applyDefault', () => {
it('returns a clone of the default if no options given', () => {
const defaults = [{ prop: 'setting' }];
const user = null;
const result = util.applyDefault(defaults, user);
const result = ESLintUtils.applyDefault(defaults, user);

expect(result).toStrictEqual(defaults);
expect(result).not.toBe(defaults);
Expand All @@ -26,7 +26,7 @@ describe('applyDefault', () => {
other: 'something',
},
];
const result = util.applyDefault(defaults, user);
const result = ESLintUtils.applyDefault(defaults, user);

expect(result).toStrictEqual([
{
Expand All @@ -44,9 +44,19 @@ describe('applyDefault', () => {
it('returns a brand new array', () => {
const defaults: undefined[] = [];
const user: undefined[] = [];
const result = util.applyDefault(defaults, user);
const result = ESLintUtils.applyDefault(defaults, user);

expect(result).not.toBe(defaults);
expect(result).not.toBe(user);
});

it('should work with array of options', () => {
const defaults: unknown[] = ['1tbs'];
const user: unknown[] = ['2tbs'];
const result = ESLintUtils.applyDefault(defaults, user);

expect(result).toStrictEqual(['2tbs']);
expect(result).not.toBe(defaults);
expect(result).not.toBe(user);
});
});
@@ -0,0 +1,81 @@
import { ESLintUtils } from '../../src';

describe('batchedSingleLineTests', () => {
const FIXTURES = `
a
b
c
`;
const errors = [
{ messageId: 'someMessage1', line: 2 },
{ messageId: 'someMessage2', line: 3 },
{ messageId: 'someMessage3', line: 4 },
];
const options = [{ optionOne: 'value' }];

it('should work without options', () => {
expect(
ESLintUtils.batchedSingleLineTests({
code: FIXTURES,
}),
).toEqual([
{ code: 'a', errors: [] },
{ code: 'b', errors: [] },
{ code: 'c', errors: [] },
]);
});

it('should work with errors', () => {
expect(
ESLintUtils.batchedSingleLineTests({
code: FIXTURES,
errors,
}),
).toEqual([
{ code: 'a', errors: [{ messageId: 'someMessage1', line: 1 }] },
{ code: 'b', errors: [{ messageId: 'someMessage2', line: 1 }] },
{ code: 'c', errors: [{ messageId: 'someMessage3', line: 1 }] },
]);
});

it('should work with all fields', () => {
const filename = 'foo.ts';
const parser = 'some-parser';

expect(
ESLintUtils.batchedSingleLineTests({
code: FIXTURES,
errors,
options,
parser,
output: FIXTURES,
filename,
}),
).toEqual([
{
code: 'a',
output: 'a',
errors: [{ messageId: 'someMessage1', line: 1 }],
parser,
filename,
options,
},
{
code: 'b',
output: 'b',
errors: [{ messageId: 'someMessage2', line: 1 }],
parser,
filename,
options,
},
{
code: 'c',
output: 'c',
errors: [{ messageId: 'someMessage3', line: 1 }],
parser,
filename,
options,
},
]);
});
});
@@ -1,10 +1,10 @@
import * as util from '../../src/eslint-utils/deepMerge';
import { ESLintUtils } from '../../src';

describe('deepMerge', () => {
it('creates a brand new object', () => {
const a = {};
const b = {};
const result = util.deepMerge(a, b);
const result = ESLintUtils.deepMerge(a, b);

expect(result).not.toBe(a);
expect(result).not.toBe(b);
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('deepMerge', () => {
},
};

expect(util.deepMerge(a, b)).toStrictEqual(Object.assign({}, a, b));
expect(ESLintUtils.deepMerge(a, b)).toStrictEqual(Object.assign({}, a, b));
});

it('deeply overwrites properties in the first one with the second', () => {
Expand All @@ -53,6 +53,6 @@ describe('deepMerge', () => {
},
};

expect(util.deepMerge(a, b)).toStrictEqual(b);
expect(ESLintUtils.deepMerge(a, b)).toStrictEqual(b);
});
});

0 comments on commit 8dcdfb1

Please sign in to comment.