This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Financial-Times/matth/write-some-tests
Write some tests
- Loading branch information
Showing
20 changed files
with
560 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/node* | ||
.circleci/ | ||
coverage/ | ||
*.env* | ||
.eslintrc.js | ||
.editorconfig | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const subject = require('../../src/filter-packages'); | ||
|
||
describe('src/filter-packages', () => { | ||
const manifests = [ | ||
{ | ||
name: 'foo', | ||
private: true | ||
}, | ||
{ | ||
name: 'bar', | ||
author: 'Joe Bloggs' | ||
}, | ||
{ | ||
name: 'baz', | ||
private: false | ||
} | ||
]; | ||
|
||
const fixture = manifests.map((manifest) => ({ manifest })); | ||
|
||
it('matches manifests with a matching field and value', () => { | ||
const result = subject('private:false', fixture); | ||
|
||
expect(result.length).toEqual(1); | ||
expect(result[0].manifest.name).toEqual('baz'); | ||
}); | ||
|
||
it('coerces different types of value', () => { | ||
const a = subject('private:true', fixture); | ||
|
||
expect(a.length).toEqual(1); | ||
expect(a[0].manifest.name).toEqual('foo'); | ||
|
||
const b = subject('author:"Joe Bloggs"', fixture); | ||
|
||
expect(b.length).toEqual(1); | ||
expect(b[0].manifest.name).toEqual('bar'); | ||
}); | ||
|
||
it('defaults to matching the package name', () => { | ||
const result = subject('baz', fixture); | ||
|
||
expect(result.length).toEqual(1); | ||
expect(result[0].manifest.name).toEqual('baz'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const mockGlob = jest.fn(); | ||
jest.mock('glob', () => mockGlob); | ||
|
||
const subject = require('../../src/get-packages'); | ||
|
||
describe('src/get-packages', () => { | ||
afterEach(() => { | ||
mockGlob.mockReset(); | ||
}); | ||
|
||
it('munges multiple patterns', () => { | ||
subject([ | ||
'components/*', | ||
'packages/*' | ||
]); | ||
|
||
expect(mockGlob).toHaveBeenCalledWith( | ||
'{components/*,packages/*}', | ||
expect.any(Object), | ||
expect.any(Function) | ||
); | ||
}); | ||
|
||
it('does not munge a single pattern', () => { | ||
subject([ 'components/*' ]); | ||
|
||
expect(mockGlob).toHaveBeenCalledWith( | ||
'components/*', | ||
expect.any(Object), | ||
expect.any(Function) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
jest.mock('fs'); | ||
|
||
const fs = require('fs'); | ||
const Subject = require('../../src/package'); | ||
|
||
const fixture = Object.freeze({ | ||
name: 'my-package', | ||
version: '0.0.0', | ||
}); | ||
|
||
describe('src/package', () => { | ||
const factory = (json) => { | ||
return new Subject(json, '/root/path/to/package'); | ||
}; | ||
|
||
describe('constructor', () => { | ||
it('stores the given manifest', () => { | ||
const instance = factory(fixture); | ||
expect(instance.manifest).toBe(fixture); | ||
}); | ||
|
||
it('stores the given location', () => { | ||
const instance = factory(fixture); | ||
expect(instance.location).toBe('/root/path/to/package'); | ||
}); | ||
}); | ||
|
||
describe('get #name', () => { | ||
it('gets the manifest name', () => { | ||
const instance = factory(fixture); | ||
expect(instance.name).toBe('my-package'); | ||
}); | ||
}); | ||
|
||
describe('get #private', () => { | ||
it('returns a boolean', () => { | ||
const instance = factory(fixture); | ||
expect(instance.private).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('get #manifestLocation', () => { | ||
it('gets the manifest name', () => { | ||
const instance = factory(fixture); | ||
expect(instance.manifestLocation).toBe('/root/path/to/package/package.json'); | ||
}); | ||
}); | ||
|
||
describe('get #nodeModulesLocation', () => { | ||
it('gets the manifest name', () => { | ||
const instance = factory(fixture); | ||
expect(instance.nodeModulesLocation).toEqual('/root/path/to/package/node_modules'); | ||
}); | ||
}); | ||
|
||
describe('#writeManifest', () => { | ||
beforeEach(() => { | ||
// The final arg is a callback that needs calling! | ||
fs.writeFile.mockImplementation((...args) => args[args.length - 1]()); | ||
}); | ||
|
||
it('writes the new manifest', async () => { | ||
const instance = factory(fixture); | ||
await instance.writeManifest({ ...fixture, version: '1.0.0' }); | ||
|
||
expect(fs.writeFile).toHaveBeenCalledWith( | ||
'/root/path/to/package/package.json', | ||
JSON.stringify({ name : 'my-package', version: '1.0.0' }, null, 2), | ||
expect.any(Function) | ||
); | ||
}); | ||
|
||
it('updates the manifest property', async () => { | ||
const instance = factory(fixture); | ||
await instance.writeManifest({ ...fixture, version: '1.0.0' }); | ||
|
||
expect(instance.manifest.version).toEqual('1.0.0'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const subject = require('../../src/sort-packages'); | ||
|
||
const fixture = Object.freeze([ | ||
{ | ||
name: 'foo', | ||
manifest: { | ||
dependencies: { | ||
qux: '0.0.0' | ||
} | ||
} | ||
}, | ||
{ | ||
name: 'bar', | ||
manifest: { | ||
dependencies: { | ||
baz: '0.0.0' | ||
} | ||
} | ||
}, | ||
{ | ||
name: 'baz', | ||
manifest: { | ||
dependencies: { | ||
foo: '0.0.0', | ||
qux: '0.0.0' | ||
} | ||
}, | ||
}, | ||
{ | ||
name: 'qux', | ||
manifest: { | ||
dependencies: { | ||
} | ||
} | ||
} | ||
]); | ||
|
||
describe('src/sort-packages', () => { | ||
it('returns a new array', () => { | ||
const result = subject(null, fixture); | ||
expect(result).not.toEqual(fixture); | ||
}); | ||
|
||
it('sorts packages topologically', () => { | ||
const result = subject(null, fixture); | ||
|
||
['qux', 'foo', 'baz', 'bar'].forEach((name, i) => { | ||
expect(result[i].name).toEqual(name); | ||
}); | ||
}); | ||
|
||
it('can reverse the sort order', () => { | ||
const result = subject(true, fixture); | ||
|
||
['bar', 'baz', 'foo', 'qux'].forEach((name, i) => { | ||
expect(result[i].name).toEqual(name); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.