'use strict'; const { expect } = require('chai'); const { v4 } = require('uuid'); const sinon = require('sinon'); const rewire = require('rewire'); const { requestEvent1, createdEvent1 } = require('../xml/event/js-requests'); const { oadrPoll1, } = require('../xml/poll/js-requests'); const { generatedFromNantumEvent1 } = require('../xml/event/js-responses'); const {sampleEvent1 } = require('../modules/nantum-responses'); describe('Event', function() { let clock; let sandbox, rewired; let fetchEventStub; after(async () => { clock.restore(); }); before(async () => { clock = sinon.useFakeTimers(new Date('2020-04-26T01:00:00.000Z').getTime()); sandbox = sinon.createSandbox(); let registration = {}; fetchEventStub = sandbox.stub().resolves(sampleEvent1); rewired = rewire('../../../processes/event.js'); rewired.__set__({ nantum: { fetchRegistration: () => Promise.resolve(registration), fetchEvent: fetchEventStub, updateRegistration: async newRegistration => { registration = newRegistration; } }, }); }); describe('retrieveEvents', function() { it('translates nantum event to oadr event', async () => { const venId = requestEvent1.venId; const commonName = v4().replace(/-/g, '').substring(0, 12); const events = await rewired.retrieveEvents(requestEvent1, commonName, venId); expect(events).to.eql(generatedFromNantumEvent1); }); }); describe('poll and updateOptType', function() { it('should return the same event on subsequent polls if it has not been opted', async () => { const venId = oadrPoll1.venId; const commonName = v4().replace(/-/g, '').substring(0, 12); const pollResponse1 = await rewired.pollForEvents(oadrPoll1, commonName, venId); expect(pollResponse1.events.length).to.eql(1); const pollResponse2 = await rewired.pollForEvents(oadrPoll1, commonName, venId); expect(pollResponse2.events.length).to.eql(1); }); it('should not return an opted event in subsequent poll response', async () => { const venId = oadrPoll1.venId; const commonName = v4().replace(/-/g, '').substring(0, 12); const pollResponse1 = await rewired.pollForEvents(oadrPoll1, commonName, venId); expect(pollResponse1.events.length).to.eql(1); await rewired.updateOptType(createdEvent1, commonName, venId); const pollResponse2 = await rewired.pollForEvents(oadrPoll1, commonName, venId); expect(pollResponse2).to.be.undefined; }); }); });