Skip to content

Commit

Permalink
feat: add spec for fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgechev committed Feb 21, 2017
1 parent fc43d73 commit 7df224d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
93 changes: 93 additions & 0 deletions test/noUnusedCssRule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {expect} from 'chai';
import {Decorator} from 'typescript';

import * as sass from 'node-sass';
Expand Down Expand Up @@ -773,4 +774,96 @@ describe('no-unused-css', () => {

});

describe('autofixes', () => {

it('should work with regular CSS', () => {
let source = `
@Component({
selector: 'foobar',
encapsulation: prefix.foo.ViewEncapsulation.Emulated,
template: \`<div></div>\`,
styles: [
\`
p {
color: red;
}
\`
]
})
class Test {}`;
const failures = assertFailure('no-unused-css', source, {
message: 'Unused styles',
startPosition: {
line: 7,
character: 12
},
endPosition: {
line: 9,
character: 13
}
}, null);
const fix = failures[0].getFix();
const replacements = fix.replacements;
expect(replacements.length).to.eq(1);
const replacement = replacements[0];
expect(replacement.text).to.eq('');
expect(replacement.start).to.eq(197);
expect(replacement.end).to.eq(240);
});

it('should work with SASS', () => {
Config.transformStyle = (source: string, url: string, d: Decorator) => {
const res = sass.renderSync({
sourceMap: true, data: source, sourceMapEmbed: true
});
const code = res.css.toString();
const base64Map = code.match(/\/\*(.*?)\*\//)[1].replace('# sourceMappingURL=data:application/json;base64,', '');
const map = JSON.parse(new Buffer(base64Map, 'base64').toString('ascii'));
return { code, source, map };
};

let source = `
@Component({
selector: 'hero-cmp',
template: \`
<h1>Hello <span>{{ hero.name }}</span></h1>
\`,
styles: [
\`
h1 {
spam {
baz {
color: red;
}
}
}
\`
]
})
class HeroComponent {
private hero: Hero;
}`;
const failures = assertFailure('no-unused-css', source, {
message: 'Unused styles',
startPosition: {
line: 8,
character: 9
},
endPosition: {
line: 12,
character: 14
}
});
Config.transformStyle = (code: string) => ({ code, map: null });
const fix = failures[0].getFix();
const replacements = fix.replacements;
expect(replacements.length).to.eq(1);
const replacement = replacements[0];
expect(replacement.text).to.eq('');
expect(replacement.start).to.eq(174);
expect(replacement.end).to.eq(261); // should be 276
});

});

});
14 changes: 10 additions & 4 deletions test/testHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as tslint from 'tslint';
import * as Lint from 'tslint';
import chai = require('chai');

interface ISourcePosition {
Expand All @@ -12,7 +13,7 @@ export interface IExpectedFailure {
endPosition: ISourcePosition;
}

function lint(ruleName: string, source: string, options): tslint.LintResult {
function lint(ruleName: string, source: string, options: any): tslint.LintResult {
let configuration = {
rules: {}
};
Expand Down Expand Up @@ -95,8 +96,8 @@ export function assertAnnotated(config: AssertConfig) {
}
};

export function assertFailure(ruleName: string, source: string, fail: IExpectedFailure, options = null) {
let result;
export function assertFailure(ruleName: string, source: string, fail: IExpectedFailure, options = null): Lint.RuleFailure[] {
let result: Lint.LintResult;
try {
result = lint(ruleName, source, options);
} catch (e) {
Expand All @@ -108,6 +109,10 @@ export function assertFailure(ruleName: string, source: string, fail: IExpectedF
chai.assert.deepEqual(fail.startPosition, ruleFail.getStartPosition().getLineAndCharacter(), 'start char doesn\'t match');
chai.assert.deepEqual(fail.endPosition, ruleFail.getEndPosition().getLineAndCharacter(), 'end char doesn\'t match');
});
if (result) {
return result.failures;
}
return undefined;
};

export function assertFailures(ruleName: string, source: string, fails: IExpectedFailure[], options = null) {
Expand All @@ -120,7 +125,8 @@ export function assertFailures(ruleName: string, source: string, fails: IExpecte
chai.assert(result.failureCount > 0, 'no failures');
result.failures.forEach((ruleFail,index) => {
chai.assert.equal(fails[index].message, ruleFail.getFailure(), 'error messages dont\'t match');
chai.assert.deepEqual(fails[index].startPosition, ruleFail.getStartPosition().getLineAndCharacter(), 'start char doesn\'t match');
chai.assert.deepEqual(fails[index].startPosition, ruleFail.getStartPosition().getLineAndCharacter(),
'start char doesn\'t match');
chai.assert.deepEqual(fails[index].endPosition, ruleFail.getEndPosition().getLineAndCharacter(), 'end char doesn\'t match');
});
};
Expand Down

0 comments on commit 7df224d

Please sign in to comment.