Skip to content

Commit 08ad874

Browse files
committed
test: add unit tests for Manifest and Packument classes
1 parent 1dff2e2 commit 08ad874

File tree

1 file changed

+115
-40
lines changed

1 file changed

+115
-40
lines changed

test/models.test.js

Lines changed: 115 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
const assert = require('assert');
2-
const { Organization } = require('../lib/organization');
1+
const assert = require('assert')
2+
const { Organization } = require('../lib/organization')
33
const { Project } = require('../lib/project')
4-
const { describe, it } = require('mocha');
5-
6-
describe('Organization', function() {
7-
it('should create an organization with a name string', function() {
8-
const org = new Organization('TestOrg');
9-
assert.strictEqual(org.name, 'TestOrg');
10-
assert.strictEqual(org.displayName, 'TestOrg');
11-
assert.strictEqual(org.url, undefined);
12-
});
13-
14-
it('should create an organization with an object', function() {
15-
const org = new Organization({ name: 'TestOrg', displayName: 'Test Organization', url: 'http://test.org' });
16-
assert.strictEqual(org.name, 'TestOrg');
17-
assert.strictEqual(org.displayName, 'Test Organization');
18-
assert.strictEqual(org.url, 'http://test.org');
19-
});
20-
21-
it('should use name as displayName if displayName is not provided', function() {
22-
const org = new Organization({ name: 'TestOrg' });
23-
assert.strictEqual(org.name, 'TestOrg');
24-
assert.strictEqual(org.displayName, 'TestOrg');
25-
assert.strictEqual(org.url, undefined);
26-
});
27-
28-
it('should handle empty object input', function() {
29-
const org = new Organization({});
30-
assert.strictEqual(org.name, undefined);
31-
assert.strictEqual(org.displayName, undefined);
32-
assert.strictEqual(org.url, undefined);
33-
});
34-
35-
it('should handle no input', function() {
36-
const org = new Organization();
37-
assert.strictEqual(org.name, undefined);
38-
assert.strictEqual(org.displayName, undefined);
39-
assert.strictEqual(org.url, undefined);
40-
});
41-
});
4+
const { Manifest, Packument } = require('../lib/npm')
5+
const { describe, it } = require('mocha')
426

7+
describe('Organization', function () {
8+
it('should create an organization with a name string', function () {
9+
const org = new Organization('TestOrg')
10+
assert.strictEqual(org.name, 'TestOrg')
11+
assert.strictEqual(org.displayName, 'TestOrg')
12+
assert.strictEqual(org.url, undefined)
13+
})
14+
15+
it('should create an organization with an object', function () {
16+
const org = new Organization({ name: 'TestOrg', displayName: 'Test Organization', url: 'http://test.org' })
17+
assert.strictEqual(org.name, 'TestOrg')
18+
assert.strictEqual(org.displayName, 'Test Organization')
19+
assert.strictEqual(org.url, 'http://test.org')
20+
})
21+
22+
it('should use name as displayName if displayName is not provided', function () {
23+
const org = new Organization({ name: 'TestOrg' })
24+
assert.strictEqual(org.name, 'TestOrg')
25+
assert.strictEqual(org.displayName, 'TestOrg')
26+
assert.strictEqual(org.url, undefined)
27+
})
28+
29+
it('should handle empty object input', function () {
30+
const org = new Organization({})
31+
assert.strictEqual(org.name, undefined)
32+
assert.strictEqual(org.displayName, undefined)
33+
assert.strictEqual(org.url, undefined)
34+
})
35+
36+
it('should handle no input', function () {
37+
const org = new Organization()
38+
assert.strictEqual(org.name, undefined)
39+
assert.strictEqual(org.displayName, undefined)
40+
assert.strictEqual(org.url, undefined)
41+
})
42+
})
4343

4444
describe('Project', () => {
4545
it('should initialize with a string repo', () => {
@@ -109,3 +109,78 @@ describe('Project', () => {
109109
assert.strictEqual(proj.packageName, null)
110110
})
111111
})
112+
113+
describe('Manifest', function () {
114+
it('should correctly initialize with given manifest data', function () {
115+
const manifestData = {
116+
name: 'test-package',
117+
version: '1.0.0',
118+
dependencies: { dep1: '^1.0.0' },
119+
optionalDependencies: { optDep1: '^1.0.0' },
120+
devDependencies: { devDep1: '^1.0.0' },
121+
peerDependencies: { peerDep1: '^1.0.0' },
122+
bundleDependencies: { bundleDep1: '^1.0.0' },
123+
bin: { bin1: 'bin1.js' }
124+
}
125+
126+
const manifest = new Manifest(manifestData)
127+
128+
assert.strictEqual(manifest.name, manifestData.name)
129+
assert.strictEqual(manifest.version, manifestData.version)
130+
assert.deepStrictEqual(manifest.dependencies, manifestData.dependencies)
131+
assert.deepStrictEqual(manifest.optionalDependencies, manifestData.optionalDependencies)
132+
assert.deepStrictEqual(manifest.devDependencies, manifestData.devDependencies)
133+
assert.deepStrictEqual(manifest.peerDependencies, manifestData.peerDependencies)
134+
assert.deepStrictEqual(manifest.bundleDependencies, manifestData.bundleDependencies)
135+
assert.deepStrictEqual(manifest.bin, manifestData.bin)
136+
})
137+
138+
it('should handle missing optional fields', function () {
139+
const manifestData = {
140+
name: 'test-package',
141+
version: '1.0.0'
142+
}
143+
144+
const manifest = new Manifest(manifestData)
145+
146+
assert.strictEqual(manifest.name, manifestData.name)
147+
assert.strictEqual(manifest.version, manifestData.version)
148+
assert.strictEqual(manifest.dependencies, undefined)
149+
assert.strictEqual(manifest.optionalDependencies, undefined)
150+
assert.strictEqual(manifest.devDependencies, undefined)
151+
assert.strictEqual(manifest.peerDependencies, undefined)
152+
assert.strictEqual(manifest.bundleDependencies, undefined)
153+
assert.strictEqual(manifest.bin, undefined)
154+
})
155+
})
156+
157+
describe('Packument', function () {
158+
it('should create an instance with the correct properties', function () {
159+
const packumentData = {
160+
name: 'example-package',
161+
distTags: { latest: '1.0.0' },
162+
modified: '2023-10-01T00:00:00.000Z',
163+
versions: { '1.0.0': {} }
164+
}
165+
166+
const packument = new Packument(packumentData)
167+
168+
assert.strictEqual(packument.name, 'example-package')
169+
assert.deepStrictEqual(packument.distTags, { latest: '1.0.0' })
170+
assert.strictEqual(packument.modified, '2023-10-01T00:00:00.000Z')
171+
assert.deepStrictEqual(packument.versions, { '1.0.0': {} })
172+
})
173+
174+
it('should handle dist-tags correctly', function () {
175+
const packumentData = {
176+
name: 'example-package',
177+
'dist-tags': { latest: '1.0.0' },
178+
modified: '2023-10-01T00:00:00.000Z',
179+
versions: { '1.0.0': {} }
180+
}
181+
182+
const packument = new Packument(packumentData)
183+
184+
assert.deepStrictEqual(packument.distTags, { latest: '1.0.0' })
185+
})
186+
})

0 commit comments

Comments
 (0)