-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathupdate.test.ts
More file actions
140 lines (106 loc) · 4.43 KB
/
Copy pathupdate.test.ts
File metadata and controls
140 lines (106 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* eslint-disable no-unused-expressions, @typescript-eslint/no-unused-expressions */
import { expect } from 'chai';
import sinon from 'sinon';
import * as utilsModule from './utils';
import { handler } from './update';
describe('atoms/update handler', () => {
let loadCurrentAtomsStub: sinon.SinonStub;
let loadCatalogStub: sinon.SinonStub;
let writeLockFileStub: sinon.SinonStub;
let consoleLogStub: sinon.SinonStub;
beforeEach(() => {
loadCurrentAtomsStub = sinon.stub(utilsModule, 'loadCurrentAtoms');
loadCatalogStub = sinon.stub(utilsModule, 'loadCatalog');
writeLockFileStub = sinon.stub(utilsModule, 'writeLockFile');
consoleLogStub = sinon.stub(console, 'log');
});
afterEach(() => {
sinon.restore();
});
it('should call loadCurrentAtoms and loadCatalog', async () => {
loadCurrentAtomsStub.resolves({});
loadCatalogStub.returns({ data: {} });
await handler();
expect(loadCurrentAtomsStub.calledOnce).to.be.true;
expect(loadCatalogStub.calledOnce).to.be.true;
});
it('should write lock file with correct atom entries', async () => {
loadCurrentAtomsStub.resolves({
Button: { version: '1.0.0', schemaHash: 'abc123' },
});
loadCatalogStub.returns({ data: {} });
await handler();
expect(writeLockFileStub.calledOnce).to.be.true;
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.atoms.Button).to.deep.equal({ version: '1.0.0', hash: 'abc123' });
});
it('should include catalog version in lock when catalog declares one', async () => {
loadCurrentAtomsStub.resolves({});
loadCatalogStub.returns({ data: { version: '2.0.0' } });
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.version).to.equal('2.0.0');
});
it('should not include version in lock when catalog has no version', async () => {
loadCurrentAtomsStub.resolves({});
loadCatalogStub.returns({ data: {} });
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.version).to.be.undefined;
});
it('should not include version in lock when catalog.data is undefined', async () => {
loadCurrentAtomsStub.resolves({});
loadCatalogStub.returns({});
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.version).to.be.undefined;
});
it('should omit atom version entry when atom has no version', async () => {
loadCurrentAtomsStub.resolves({
Card: { version: undefined, schemaHash: 'def456' },
});
loadCatalogStub.returns({ data: {} });
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.atoms.Card).to.deep.equal({ hash: 'def456' });
expect(lock.atoms.Card.version).to.be.undefined;
});
it('should omit atom version entry when atom version is empty string', async () => {
loadCurrentAtomsStub.resolves({
Card: { version: '', schemaHash: 'def456' },
});
loadCatalogStub.returns({ data: {} });
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.atoms.Card.version).to.be.undefined;
});
it('should set generated to a valid ISO timestamp', async () => {
loadCurrentAtomsStub.resolves({});
loadCatalogStub.returns({ data: {} });
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(lock.generated).to.be.a('string');
expect(new Date(lock.generated).toISOString()).to.equal(lock.generated);
});
it('should write a lock with multiple atoms', async () => {
loadCurrentAtomsStub.resolves({
Button: { version: '1.0.0', schemaHash: 'hash1' },
Card: { version: undefined, schemaHash: 'hash2' },
Banner: { version: '0.5.0', schemaHash: 'hash3' },
});
loadCatalogStub.returns({ data: { version: '3.0.0' } });
await handler();
const lock = writeLockFileStub.firstCall.args[0];
expect(Object.keys(lock.atoms)).to.have.length(3);
expect(lock.atoms.Button).to.deep.equal({ version: '1.0.0', hash: 'hash1' });
expect(lock.atoms.Card).to.deep.equal({ hash: 'hash2' });
expect(lock.atoms.Banner).to.deep.equal({ version: '0.5.0', hash: 'hash3' });
expect(lock.version).to.equal('3.0.0');
});
it('should log success message after writing', async () => {
loadCurrentAtomsStub.resolves({});
loadCatalogStub.returns({ data: {} });
await handler();
expect(consoleLogStub.calledWith('[atoms update] Lock file updated successfully.')).to.be.true;
});
});