Skip to content

Commit

Permalink
chore: add more code actions unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Jan 27, 2024
1 parent c5e057a commit d8b2d5f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 205 deletions.
202 changes: 0 additions & 202 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"lint": "xo && npm run lint:md",
"lint:md": "prettier --parser markdown '**/*.md' --check",
"prepare": "husky install",
"test": "NODE_NO_WARNINGS=1 node --require tsx/cjs --test test/index.ts | tap-mocha-reporter spec",
"test": "NODE_NO_WARNINGS=1 node --require tsx/cjs --test test/index.ts",
"test:coverage": "NODE_NO_WARNINGS=1 c8 node --require tsx/cjs --test test/index.ts",
"test:watch": "NODE_NO_WARNINGS=1 node --require tsx/cjs --watch --test test/index.ts",
"vscode:prepublish": "npm run check && npm run build"
Expand Down Expand Up @@ -213,7 +213,6 @@
"prettier": "^3.2.4",
"prettier-plugin-packagejson": "^2.4.9",
"rimraf": "^5.0.5",
"tap-mocha-reporter": "^5.0.4",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
"vsce": "^2.15.0",
Expand Down
91 changes: 91 additions & 0 deletions test/code-actions-builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {test, describe} from 'node:test';
import assert from 'node:assert';
import {TextDocument} from 'vscode-languageserver-textdocument';
import {
CodeAction,
Diagnostic,
Range,
Position,
DiagnosticSeverity,
CodeActionKind,
uinteger,
type CodeActionParams
} from 'vscode-languageserver';
import {QuickFixCodeActionsBuilder} from '../server/code-actions-builder';

const testTextDocument: TextDocument = TextDocument.create(
'file:///test.js',
'javascript',
1,
'const foo = 1;\nconst bar = 2;\n'
);

describe('QuickFixCodeActionsBuilder:', () => {
test('Server is a function', (t) => {
assert.strictEqual(typeof QuickFixCodeActionsBuilder, 'function');
});

test('ignores non xo code actions', (t) => {
const diagnostic = Diagnostic.create(
Range.create(Position.create(0, 0), Position.create(0, 0)),
'test message',
DiagnosticSeverity.Error,
'test',
'non-xo'
);

const builder = new QuickFixCodeActionsBuilder(testTextDocument, [diagnostic], undefined);

const codeAction = builder.build();

assert.deepStrictEqual(codeAction, []);
});

describe('Disable rule actions:', () => {
const diagnostic = Diagnostic.create(
Range.create(Position.create(0, 0), Position.create(0, 0)),
'test message',
DiagnosticSeverity.Error,
'test-rule',
'XO'
);

const builder = new QuickFixCodeActionsBuilder(testTextDocument, [diagnostic], undefined);
test('Creates ignore same line code action', (t) => {
const codeActions = builder.build();
assert.equal(Array.isArray(codeActions) && codeActions.length === 3, true);
const codeAction = codeActions.find(
(action) => action.title === `Ignore Rule ${diagnostic.code}: Same Line`
);
assert.strictEqual(codeAction?.kind, CodeActionKind.QuickFix);
assert.strictEqual(
codeAction?.edit?.changes?.[testTextDocument.uri]?.[0].newText,
` // eslint-disable-line ${diagnostic.code}`
);
});

test('Creates ignore line above code action', (t) => {
const codeActions = builder.build();
const codeAction = codeActions.find(
(action) => action.title === `Ignore Rule ${diagnostic.code}: Line Above`
);
assert.strictEqual(codeAction?.kind, CodeActionKind.QuickFix);
assert.strictEqual(
codeAction?.edit?.changes?.[testTextDocument.uri]?.[0].newText,
`// eslint-disable-next-line ${diagnostic.code}\n`
);
});

test('Creates ignore entire file code action', (t) => {
const codeActions = builder.build();
const codeAction = codeActions.find(
(action) => action.title === `Ignore Rule ${diagnostic.code}: Entire File`
);
assert.strictEqual(codeAction?.kind, CodeActionKind.QuickFix);
assert.strictEqual(
codeAction?.edit?.changes?.[testTextDocument.uri]?.[0].newText,
`/* eslint-disable ${diagnostic.code} */\n`
);
});
});
});
1 change: 1 addition & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import './server.test.js';
import './lsp/document-sync.test.js';
import './lsp/initialization.test.js';
import './lsp/code-actions.test.js';
import './code-actions-builder.test.js';
Loading

0 comments on commit d8b2d5f

Please sign in to comment.