diff --git a/core/server/services/stripe/index.js b/core/server/services/stripe/index.js index eb47e8680e..198d1c5c96 100644 --- a/core/server/services/stripe/index.js +++ b/core/server/services/stripe/index.js @@ -27,7 +27,10 @@ function configureApi() { } } -const debouncedConfigureApi = _.debounce(configureApi, 600); +const debouncedConfigureApi = _.debounce(() => { + configureApi(); + events.emit('services.stripe.reconfigured'); +}, 600); module.exports = { async init() { @@ -37,7 +40,6 @@ module.exports = { return; } debouncedConfigureApi(); - events.emit('services.stripe.reconfigured'); }); }, diff --git a/test/regression/services/stripe.test.js b/test/regression/services/stripe.test.js new file mode 100644 index 0000000000..8d02f8b2f1 --- /dev/null +++ b/test/regression/services/stripe.test.js @@ -0,0 +1,38 @@ +const sinon = require('sinon'); +const rewire = require('rewire'); + +const events = require('events'); + +const rewiredStripeService = rewire('../../../core/server/services/stripe'); + +describe('Stripe Service', function () { + beforeEach(function () { + this.clock = sinon.useFakeTimers(); + }); + + afterEach(function () { + this.clock.restore(); + }); + + it('Emits a "services.stripe.reconfigured" event when it is reconfigured', async function () { + const eventsStub = new events.EventEmitter(); + const configureApiStub = sinon.spy(); + + const emitReconfiguredEventSpy = sinon.spy(eventsStub, 'emit').withArgs('services.stripe.reconfigured'); + + rewiredStripeService.__set__('events', eventsStub); + + await rewiredStripeService.init(); + + // This is _after_ init, because init calls configureApi, and we DGAF about that call. + rewiredStripeService.__set__('configureApi', configureApiStub); + + eventsStub.emit('settings.edited', { + get: sinon.stub().withArgs('key').returns('stripe_connect_secret_key') + }); + + this.clock.tick(600); + + sinon.assert.callOrder(configureApiStub, emitReconfiguredEventSpy); + }); +});