diff --git a/src/intents.js b/src/intents.js index e44b4d87..211d8321 100644 --- a/src/intents.js +++ b/src/intents.js @@ -145,7 +145,7 @@ export function createService (cozy, intentId, serviceWindow) { let terminated = false const terminate = (message) => { - if (terminated) throw new Error('Intent service has been terminated') + if (terminated) throw new Error('Intent service has already been terminated') terminated = true serviceWindow.parent.postMessage(message, intent.attributes.client) } diff --git a/test/unit/intents.js b/test/unit/intents.js index 1455c697..afeba243 100644 --- a/test/unit/intents.js +++ b/test/unit/intents.js @@ -404,6 +404,7 @@ describe('Intents', function () { windowMock.parent.postMessage .withArgs(messageMatch, expectedIntent.attributes.client).calledOnce.should.be.true() }) + it('should calculate width and height from a provided dom element', async function () { const windowMock = mockWindow() @@ -437,7 +438,39 @@ describe('Intents', function () { windowMock.parent.postMessage .withArgs(messageMatch, expectedIntent.attributes.client).calledOnce.should.be.true() }) + + it('should not be called once the service has been terminated', async function () { + const windowMock = mockWindow() + + const clientHandshakeEventMessageMock = { + origin: expectedIntent.attributes.client, + data: { foo: 'bar' } + } + + const result = { + type: 'io.cozy.things' + } + + windowMock.parent.postMessage.callsFake(() => { + const messageEventListener = windowMock.addEventListener.secondCall.args[1] + messageEventListener(clientHandshakeEventMessageMock) + }) + + const service = await cozy.client.intents.createService(expectedIntent._id, windowMock) + + service.terminate(result) + + should.throws(() => { + service.resizeClient({ + element: { + clientHeight: 10, + clientWidth: 13 + } + }) + }, /Intent service has been terminated/) + }) }) + describe('Terminate', function () { it('should send result document to Client', async function () { const windowMock = mockWindow() @@ -524,7 +557,7 @@ describe('Intents', function () { should.throws(() => { service.terminate(result) - }, /Intent service has been terminated/) + }, /Intent service has already been terminated/) }) }) @@ -596,7 +629,7 @@ describe('Intents', function () { should.throws(() => { service.cancel() - }, /Intent service has been terminated/) + }, /Intent service has already been terminated/) }) it('should forbbid further calls to terminate()', async function () { @@ -622,7 +655,7 @@ describe('Intents', function () { should.throws(() => { service.terminate(result) - }, /Intent service has been terminated/) + }, /Intent service has already been terminated/) }) }) @@ -680,7 +713,7 @@ describe('Intents', function () { should.throws(() => { service.cancel() - }, /Intent service has been terminated/) + }, /Intent service has already been terminated/) }) it('should forbbid further calls to terminate()', async function () { @@ -706,7 +739,7 @@ describe('Intents', function () { should.throws(() => { service.terminate(result) - }, /Intent service has been terminated/) + }, /Intent service has already been terminated/) }) }) })