Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Add spec for writeManifest method of package module
Browse files Browse the repository at this point in the history
 🐿 v2.10.0
  • Loading branch information
i-like-robots committed Sep 25, 2018
1 parent 1f9e0bc commit 9d9cfc9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ const writeFile = util.promisify(fs.writeFile);

class Package {
constructor (manifest, location) {
// define read-only properties
Object.defineProperties(this, {
manifest: {
value: Object.freeze(manifest)
},
location: {
value: path.normalize(location)
}
});
this.manifest = Object.freeze(manifest);
this.location = path.normalize(location);
}

get name () {
Expand All @@ -41,7 +34,7 @@ class Package {
async writeManifest (manifest) {
const json = JSON.stringify(manifest, null, 2);
await writeFile(this.manifestLocation, json);
this.manifest = manifest;
this.manifest = Object.freeze(manifest);
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/src/package.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
jest.mock('fs');

const fs = require('fs');
const Subject = require('../../src/package');

const fixture = Object.freeze({
Expand Down Expand Up @@ -49,4 +52,29 @@ describe('src/package', () => {
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');
});
});
});

0 comments on commit 9d9cfc9

Please sign in to comment.