import sinon, { SinonStub } from 'sinon'; import { expect } from 'chai'; import * as pushExports from '../push'; import * as readAuthAndConfigExports from '../../helpers/read-auth-and-config'; import { rootConfigFileExample } from '../../../test-utils/test-root-funeral-pm'; import * as postProductModuleDefinitionExports from '../../helpers/post-product-module-definition'; import * as checkValidProductModuleDirectoryExports from '../../helpers/check-valid-product-module-directory'; import * as readProductModuleDefinitionExports from '../../helpers/read-product-module-definition'; import * as validateDocumentsExports from '../../helpers/validate-documents'; import * as validateProductModuleDefinitionExports from '../../helpers/validate-product-module-definition'; import * as productModuleDefinitionDomainExports from '../../domain/product-module-definition'; describe('rp push', function () { const sandbox = sinon.createSandbox(); let pushCollectionModuleStub: SinonStub; let pushProductModuleStub: SinonStub; beforeEach(function () { pushCollectionModuleStub = sandbox.stub(pushExports, 'pushCollectionModule'); pushProductModuleStub = sandbox.stub(pushExports, 'pushProductModule'); sandbox.stub(validateProductModuleDefinitionExports, 'validateProductModuleDefinition').resolves(); sandbox.stub(checkValidProductModuleDirectoryExports, 'checkValidProductModuleDirectory').resolves(); sandbox.stub(readProductModuleDefinitionExports, 'readProductModuleDefinition').resolves({ // @ts-ignore — minimal shape needed for pushProductModule's local validators workflows: { alterationHooks: [], applicationAlterationHooks: [], memberAlterationHooks: [] }, }); // @ts-ignore sandbox.stub(validateDocumentsExports, 'validateDocuments').resolves({}); // @ts-ignore sandbox.stub(productModuleDefinitionDomainExports, 'productModuleDefinitionToNetwork').returns({}); // @ts-ignore sandbox.stub(productModuleDefinitionDomainExports, 'productModuleDefinitionFromNetwork').returns({}); }); afterEach(function () { // Restore the original functions after each test sandbox.verifyAndRestore(); }); it('should call pushCollectionModule when reading collection module config', async function () { sandbox.stub(readAuthAndConfigExports, 'readAuthAndConfig').returns({ apiKey: 'string', config: { collectionModuleKey: 'blank_starter_template', collectionModuleName: 'Blank Starter Template', organizationId: '00000000-0000-0000-0000-000000000001', host: 'http://localhost:4000', settings: {}, manualTransactions: [ { key: 'raise_premium', functionName: 'raisePremium', displayData: { type: '{{ policy.module.type }}', sum_assured: '{{ zarString policy.sum_assured }}', amount: '{{ zarString policy.module.cover_amount }}', name: '{{ policyholder.first_name }}', last_name: '{{ policyholder.last_name }}', payment_method_type: '{{ payment_method.type }}', }, }, { key: 'add_collection', functionName: 'addCollection', displayData: undefined, }, ], }, }); await pushExports.push({}); sandbox.assert.calledOnce(pushCollectionModuleStub); sandbox.assert.notCalled(pushProductModuleStub); }); it('should call pushProductModule when reading product module config', async function () { sandbox.stub(readAuthAndConfigExports, 'readAuthAndConfig').returns({ apiKey: 'string', config: rootConfigFileExample, }); await pushExports.push({}); sandbox.assert.calledOnce(pushProductModuleStub); sandbox.assert.notCalled(pushCollectionModuleStub); }); it('should call postProductModuleDefinition with the target product module key when the target is provided', async function () { pushProductModuleStub.restore(); sandbox.stub(readAuthAndConfigExports, 'readAuthAndConfig').returns({ apiKey: 'string', config: rootConfigFileExample, }); const postProductModuleDefinitionStub = sandbox .stub(postProductModuleDefinitionExports, 'postProductModuleDefinition') // @ts-ignore .returns({}); await pushExports.push({ target: 'different_module', force: true, sort: false }); const lastArgs = postProductModuleDefinitionStub.lastCall.args; expect(postProductModuleDefinitionStub.calledOnce).to.equals(true); expect(lastArgs[0].productModuleKey).to.equal('different_module'); }); });