Skip to content

Commit

Permalink
style(lint): lint test files
Browse files Browse the repository at this point in the history
  • Loading branch information
liangchunn committed Oct 16, 2018
1 parent 3825f21 commit dd659c2
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 182 deletions.
63 changes: 41 additions & 22 deletions test/unit/CancellationToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ var beforeEach = require('mocha').beforeEach;
var afterEach = require('mocha').afterEach;
var expect = require('chai').expect;
var mockFs = require('mock-fs');
var CancellationToken = require('../../lib/CancellationToken').CancellationToken;
var CancellationToken = require('../../lib/CancellationToken')
.CancellationToken;

describe('[UNIT] CancellationToken', function () {
beforeEach(function () {
describe('[UNIT] CancellationToken', function() {
beforeEach(function() {
var fsTree = {};
fsTree[os.tmpdir()] = mockFs.directory();

mockFs(fsTree);
});

afterEach(function () {
afterEach(function() {
mockFs.restore();
});

it('should create valid cancellation token', function () {
it('should create valid cancellation token', function() {
var tokenA = new CancellationToken();
expect(tokenA.isCancellationRequested()).to.be.false;

Expand All @@ -35,42 +36,54 @@ describe('[UNIT] CancellationToken', function () {
expect(tokenD.isCancellationRequested()).to.be.true;
});

it('should serialize to JSON', function () {
it('should serialize to JSON', function() {
var tokenA = new CancellationToken();
var json = JSON.stringify(tokenA);

expect(json).to.be.a('string');
expect(function() { JSON.parse(json); }).to.not.throw(Error);
expect(function() {
JSON.parse(json);
}).to.not.throw(Error);
expect(JSON.parse(json)).to.be.a('object');

var tokenB = CancellationToken.createFromJSON(JSON.parse(json));
expect(tokenA.getCancellationFilePath()).to.be.equal(tokenB.getCancellationFilePath());
expect(tokenA.isCancellationRequested()).to.be.equal(tokenB.isCancellationRequested());
expect(tokenA.getCancellationFilePath()).to.be.equal(
tokenB.getCancellationFilePath()
);
expect(tokenA.isCancellationRequested()).to.be.equal(
tokenB.isCancellationRequested()
);
});

it('should generate path in os.tmpdir() directory', function () {
it('should generate path in os.tmpdir() directory', function() {
var tokenA = new CancellationToken();

expect(tokenA.getCancellationFilePath().indexOf(os.tmpdir())).to.be.equal(0);
expect(tokenA.getCancellationFilePath().indexOf(os.tmpdir())).to.be.equal(
0
);
});

it('should throw ts.OperationCanceledException error on cancelled', function () {
it('should throw ts.OperationCanceledException error on cancelled', function() {
var tokenA = new CancellationToken();
expect(function () { tokenA.throwIfCancellationRequested(); }).to.not.throw();
expect(function() {
tokenA.throwIfCancellationRequested();
}).to.not.throw();

var tokenB = new CancellationToken('rgeer#R23r$#T$3t#$t43', true);
expect(function () { tokenB.throwIfCancellationRequested(); }).to.throw(ts.OperationCanceledException);
expect(function() {
tokenB.throwIfCancellationRequested();
}).to.throw(ts.OperationCanceledException);
});

it('should write file in filesystem on requestCancellation', function () {
it('should write file in filesystem on requestCancellation', function() {
var tokenA = new CancellationToken();
tokenA.requestCancellation();

expect(tokenA.isCancellationRequested()).to.be.true;
expect(fs.existsSync(tokenA.getCancellationFilePath())).to.be.true;
});

it('should cleanup file on cleanupCancellation', function () {
it('should cleanup file on cleanupCancellation', function() {
var tokenA = new CancellationToken();
tokenA.requestCancellation();
tokenA.cleanupCancellation();
Expand All @@ -79,18 +92,24 @@ describe('[UNIT] CancellationToken', function () {
expect(fs.existsSync(tokenA.getCancellationFilePath())).to.be.false;

// make sure we can call it as many times as we want to
expect(function() { tokenA.cleanupCancellation(); }).to.not.throw(Error);
expect(function() {
tokenA.cleanupCancellation();
}).to.not.throw(Error);
expect(tokenA.isCancellationRequested()).to.be.false;
});

it('should not throw error on cleanupCancellation with no file exists', function () {
it('should not throw error on cleanupCancellation with no file exists', function() {
var tokenA = new CancellationToken('some_file_that_doesnt_exists', true);

expect(function() { tokenA.cleanupCancellation(); }).to.not.throw();
expect(function() { tokenA.cleanupCancellation(); }).to.not.throw();
expect(function() {
tokenA.cleanupCancellation();
}).to.not.throw();
expect(function() {
tokenA.cleanupCancellation();
}).to.not.throw();
});

it('should throttle check for 10ms', function (done) {
it('should throttle check for 10ms', function(done) {
var tokenA = new CancellationToken();
var tokenB = CancellationToken.createFromJSON(tokenA.toJSON());
var start = Date.now();
Expand All @@ -107,7 +126,7 @@ describe('[UNIT] CancellationToken', function () {
expect(tokenB.isCancellationRequested()).to.be.false;
}

setTimeout(function () {
setTimeout(function() {
expect(tokenB.isCancellationRequested()).to.be.true;
done();
}, 11);
Expand Down
53 changes: 34 additions & 19 deletions test/unit/FileRegister.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ var beforeEach = require('mocha').beforeEach;
var expect = require('chai').expect;
var FilesRegister = require('../../lib/FilesRegister').FilesRegister;

describe('[UNIT] FilesRegister', function () {
describe('[UNIT] FilesRegister', function() {
var register;
beforeEach(function () {
register = new FilesRegister(function () {
beforeEach(function() {
register = new FilesRegister(function() {
return {
test: true
};
});
});

it('should add and remove files', function () {
it('should add and remove files', function() {
register.add('/test');
register.add('/test2');
expect(register.has('/test')).to.be.true;
Expand All @@ -23,37 +23,52 @@ describe('[UNIT] FilesRegister', function () {
expect(register.has('/test')).to.be.false;
expect(register.has('/test2')).to.be.true;

expect(function() { register.remove('/test'); }).to.not.throw();
expect(function() {
register.remove('/test');
}).to.not.throw();
register.remove('/test2');
expect(register.has('/test')).to.be.false;
expect(register.has('/test2')).to.be.false;
});

it('should get file that exists in register', function () {
it('should get file that exists in register', function() {
register.add('/test');
expect(function() { register.get('/test'); }).to.not.throw();
expect(function() { register.get('/test2'); }).to.throw();
expect(function() {
register.get('/test');
}).to.not.throw();
expect(function() {
register.get('/test2');
}).to.throw();
expect(register.get('/test')).to.be.a('object');
expect(Object.keys(register.get('/test'))).to.be.deep.equal(['mtime', 'data']);
expect(Object.keys(register.get('/test'))).to.be.deep.equal([
'mtime',
'data'
]);
});

it('should list all keys in register', function () {
it('should list all keys in register', function() {
register.add('/test');
register.add('/test/foo');
register.add('/test/foo/bar');
expect(register.keys()).to.be.deep.equal(['/test', '/test/foo', '/test/foo/bar']);
expect(register.keys()).to.be.deep.equal([
'/test',
'/test/foo',
'/test/foo/bar'
]);

register.remove('/test');
expect(register.keys()).to.be.deep.equal(['/test/foo', '/test/foo/bar']);
});

it('should get data from file', function () {
it('should get data from file', function() {
register.add('/test');
expect(register.getData('/test')).to.be.deep.equal({ test: true });
expect(function() { register.getData('/test2'); }).to.throw(Error);
expect(function() {
register.getData('/test2');
}).to.throw(Error);
});

it('should ensure if file exists', function () {
it('should ensure if file exists', function() {
expect(register.has('/test')).to.be.false;
register.ensure('/test');
expect(register.has('/test')).to.be.true;
Expand All @@ -63,20 +78,20 @@ describe('[UNIT] FilesRegister', function () {
expect(reference).to.be.equal(register.get('/test'));
});

it('should mutate existing data', function () {
it('should mutate existing data', function() {
register.add('/test');
var dataReference = register.getData('/test');
expect(dataReference.test).to.be.true;
register.mutateData('/test', function (data) {
register.mutateData('/test', function(data) {
data.test = false;
});
expect(dataReference).to.be.equal(register.getData('/test'));
expect(dataReference.test).to.be.false;
});

it('should set mtime and reset data if mtime changes', function () {
it('should set mtime and reset data if mtime changes', function() {
register.add('/test');
register.mutateData('/test', function (data) {
register.mutateData('/test', function(data) {
data.test = false;
});
expect(register.getData('/test').test).to.be.false;
Expand All @@ -85,7 +100,7 @@ describe('[UNIT] FilesRegister', function () {
register.setMtime('/test', 1000);
expect(register.getMtime('/test')).to.be.equal(1000);
expect(register.getData('/test').test).to.be.true;
register.mutateData('/test', function (data) {
register.mutateData('/test', function(data) {
data.test = false;
});
expect(register.getData('/test').test).to.be.false;
Expand Down
48 changes: 26 additions & 22 deletions test/unit/FilesWatcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var sinon = require('sinon');
var expect = require('chai').expect;
var mockRequire = require('mock-require');

describe('[UNIT] FilesWatcher', function () {
describe('[UNIT] FilesWatcher', function() {
var FilesWatcher;
var watcher;
var watchStub;
var watcherStub;

beforeEach(function () {
beforeEach(function() {
watcherStub = {
on: sinon.stub().returnsThis()
};
Expand All @@ -21,24 +21,21 @@ describe('[UNIT] FilesWatcher', function () {
mockRequire('chokidar', { watch: watchStub });
FilesWatcher = mockRequire.reRequire('../../lib/FilesWatcher').FilesWatcher;

watcher = new FilesWatcher(
['/test', '/bar'],
['.ext1', '.ext2']
);
watcher = new FilesWatcher(['/test', '/bar'], ['.ext1', '.ext2']);
});

afterEach(function () {
afterEach(function() {
mockRequire.stopAll();
});

it('should check if file is supported', function () {
it('should check if file is supported', function() {
expect(watcher.isFileSupported('/foo.ext1')).to.be.true;
expect(watcher.isFileSupported('/foo.ext2')).to.be.true;
expect(watcher.isFileSupported('/foo.txt')).to.be.false;
expect(watcher.isFileSupported('/foo.ext1.txt')).to.be.false;
});

it('should check if is watching file', function () {
it('should check if is watching file', function() {
expect(watcher.isWatchingFile('/test/a.ext1')).to.be.false;
expect(watcher.isWatchingFile('/test/a.txt')).to.be.false;
expect(watcher.isWatchingFile('/test')).to.be.false;
Expand All @@ -52,18 +49,18 @@ describe('[UNIT] FilesWatcher', function () {
expect(watcher.isWatchingFile('/foo/a.ext1')).to.be.false;
});

it('should check if watcher is watching', function () {
it('should check if watcher is watching', function() {
expect(watcher.isWatching()).to.be.false;
watcher.watch();
expect(watcher.isWatching()).to.be.true;
expect(function () { watcher.watch(); }).to.throw(Error);
expect(function() {
watcher.watch();
}).to.throw(Error);
});

it('should add and remove listeners', function () {
var listenerA = function () {
};
var listenerB = function () {
};
it('should add and remove listeners', function() {
var listenerA = function() {};
var listenerB = function() {};

expect(watcher.listeners).to.be.a('object');
watcher.on('event', listenerA);
Expand All @@ -74,19 +71,23 @@ describe('[UNIT] FilesWatcher', function () {
watcher.off('event', listenerA);
expect(watcher.listeners['event']).to.be.deep.equal([listenerB]);

expect(function() { watcher.off('event', listenerA); }).to.not.throw();
expect(function() {
watcher.off('event', listenerA);
}).to.not.throw();
expect(watcher.listeners['event']).to.be.deep.equal([listenerB]);

watcher.off('event', listenerB);
expect(watcher.listeners['event']).to.be.deep.equal([]);

expect(watcher.listeners['foo']).to.be.undefined;
expect(function() { watcher.off('foo', listenerA); }).to.not.throw();
expect(function() {
watcher.off('foo', listenerA);
}).to.not.throw();

expect(watcher.listeners['foo']).to.be.undefined;
});

it('should watch filesystem using chokidar', function () {
it('should watch filesystem using chokidar', function() {
expect(watchStub.called).to.be.false;

var changeListenerA = sinon.spy();
Expand All @@ -105,8 +106,12 @@ describe('[UNIT] FilesWatcher', function () {
expect(triggerChange).to.be.a('function');
expect(triggerUnlink).to.be.a('function');

expect(function() { triggerChange('/test/test.ext1', {}); }).to.not.throw();
expect(function() { triggerUnlink('/test/test.ext1', {}); }).to.not.throw();
expect(function() {
triggerChange('/test/test.ext1', {});
}).to.not.throw();
expect(function() {
triggerUnlink('/test/test.ext1', {});
}).to.not.throw();

watcher.on('change', changeListenerA);
watcher.on('change', changeListenerB);
Expand All @@ -126,7 +131,6 @@ describe('[UNIT] FilesWatcher', function () {
expect(changeListenerB.called).to.be.true;
expect(changeListenerB.called).to.be.true;


// manually trigger unlink listeners
triggerUnlink('/test/test.txt');
expect(unlinkListenerA.called).to.be.false;
Expand Down
Loading

0 comments on commit dd659c2

Please sign in to comment.