|
| 1 | +import crypto from 'node:crypto'; |
| 2 | + |
1 | 3 | import { ModuleInstantiationError } from '../../../../../../src/devcomp/domain/errors.js';
|
2 | 4 | import { Module } from '../../../../../../src/devcomp/domain/models/module/Module.js';
|
3 | 5 | import { DomainError } from '../../../../../../src/shared/domain/errors.js';
|
4 |
| -import { catchErrSync, expect } from '../../../../../test-helper.js'; |
| 6 | +import { catchErrSync, expect, sinon } from '../../../../../test-helper.js'; |
5 | 7 |
|
6 | 8 | describe('Unit | Devcomp | Domain | Models | Module | Module', function () {
|
7 | 9 | describe('#constructor', function () {
|
@@ -138,4 +140,39 @@ describe('Unit | Devcomp | Domain | Models | Module | Module', function () {
|
138 | 140 | });
|
139 | 141 | });
|
140 | 142 | });
|
| 143 | + |
| 144 | + describe('#hash', function () { |
| 145 | + it('returns hash of module content', function () { |
| 146 | + // given |
| 147 | + const id = 1; |
| 148 | + const slug = 'les-adresses-email'; |
| 149 | + const title = 'Les adresses email'; |
| 150 | + const isBeta = false; |
| 151 | + const grains = [Symbol('text')]; |
| 152 | + const transitionTexts = []; |
| 153 | + const details = Symbol('details'); |
| 154 | + const module = new Module({ id, slug, title, isBeta, grains, details, transitionTexts }); |
| 155 | + const hashedResult = 'AZERTY123456'; |
| 156 | + |
| 157 | + const digestStub = sinon.stub().returns(hashedResult); |
| 158 | + const updateStub = sinon.stub(); |
| 159 | + const createHashStub = sinon.stub(crypto, 'createHash').returns({ |
| 160 | + copy: () => { |
| 161 | + return { |
| 162 | + digest: digestStub, |
| 163 | + }; |
| 164 | + }, |
| 165 | + update: updateStub, |
| 166 | + }); |
| 167 | + |
| 168 | + // when |
| 169 | + const result = module.hash(); |
| 170 | + |
| 171 | + // then |
| 172 | + expect(result).to.deep.equal(hashedResult); |
| 173 | + expect(createHashStub).to.have.been.calledOnceWith('sha256'); |
| 174 | + expect(updateStub).to.have.been.calledOnceWith(JSON.stringify(module)); |
| 175 | + expect(digestStub).to.have.been.calledOnceWith('hex'); |
| 176 | + }); |
| 177 | + }); |
141 | 178 | });
|
0 commit comments