diff --git a/src/lib/stack-client.js b/src/lib/stack-client.js index eb0ee027f..4339d4c0e 100644 --- a/src/lib/stack-client.js +++ b/src/lib/stack-client.js @@ -353,13 +353,25 @@ const getSettingsAppURL = function() { * @returns {Promise} */ const init = function({ cozyClient: client, onCreate, onDelete }) { - cozyClient = client - if (!cozyClient.isLogged) return - initializeRealtime({ - getApp, - onCreate, - onDelete, - cozyClient + let terminateRealtime + cozyClient = client // Setting global variable + const onLogin = () => { + terminateRealtime = initializeRealtime({ + getApp, + onCreate, + onDelete, + cozyClient + }) + } + if (cozyClient.isLogged) { + onLogin() + } else { + cozyClient.once('login', onLogin) + } + cozyClient.once('logout', () => { + if (terminateRealtime) { + terminateRealtime() + } }) } diff --git a/test/lib/stack-client/stack-client.appiconprops.spec.js b/test/lib/stack-client/stack-client.appiconprops.spec.js index a46639e62..83d21d521 100644 --- a/test/lib/stack-client/stack-client.appiconprops.spec.js +++ b/test/lib/stack-client/stack-client.appiconprops.spec.js @@ -1,7 +1,7 @@ /* global __TARGET__ */ +import CozyClient from 'cozy-client' import stack from 'lib/stack-client' - import internal from 'lib/stack-internal' let oldTarget @@ -15,9 +15,9 @@ describe("stack client", () => { uri: "https://test.mycozy.cloud", } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.cozyfetchjson.spec.js b/test/lib/stack-client/stack-client.cozyfetchjson.spec.js index d9a47e8f3..6a631000f 100644 --- a/test/lib/stack-client/stack-client.cozyfetchjson.spec.js +++ b/test/lib/stack-client/stack-client.cozyfetchjson.spec.js @@ -1,4 +1,5 @@ import stack from 'lib/stack-client' +import CozyClient from 'cozy-client' describe("stack client", () => { @@ -14,15 +15,15 @@ describe("stack client", () => { }, json }) - let cozyClient = { - getStackClient: () => { - return { - token: { token: "mytoken"}, - uri: "https://test.mycozy.cloud", - fetch - } + + let cozyClient = new CozyClient({ + stackClient: { + token: { token: "mytoken"}, + uri: "https://test.mycozy.cloud", + fetch } - } + }) + let params = { cozyClient, onCreateApp: function() {}, diff --git a/test/lib/stack-client/stack-client.cozyurl.spec.js b/test/lib/stack-client/stack-client.cozyurl.spec.js index 47087e90c..7206c3f04 100644 --- a/test/lib/stack-client/stack-client.cozyurl.spec.js +++ b/test/lib/stack-client/stack-client.cozyurl.spec.js @@ -1,5 +1,5 @@ import stack from 'lib/stack-client' - +import CozyClient from 'cozy-client' import internal from 'lib/stack-internal' describe("stack client", () => { @@ -11,9 +11,9 @@ describe("stack client", () => { uri: "https://test.mycozy.cloud", } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.getapp.spec.js b/test/lib/stack-client/stack-client.getapp.spec.js index 5e412ca96..cf759fed5 100644 --- a/test/lib/stack-client/stack-client.getapp.spec.js +++ b/test/lib/stack-client/stack-client.getapp.spec.js @@ -1,3 +1,4 @@ +import CozyClient from 'cozy-client' import stack from 'lib/stack-client' import internal from 'lib/stack-internal' @@ -39,9 +40,9 @@ describe("stack client", () => { }) } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.getapps.spec.js b/test/lib/stack-client/stack-client.getapps.spec.js index 7d99bf59a..700e51773 100644 --- a/test/lib/stack-client/stack-client.getapps.spec.js +++ b/test/lib/stack-client/stack-client.getapps.spec.js @@ -1,5 +1,5 @@ +import CozyClient from 'cozy-client' import stack from 'lib/stack-client' - import internal from 'lib/stack-internal' describe("stack client", () => { @@ -39,9 +39,9 @@ describe("stack client", () => { ) } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.getcontext.spec.js b/test/lib/stack-client/stack-client.getcontext.spec.js index a5c2fdcd1..d3f715dcb 100644 --- a/test/lib/stack-client/stack-client.getcontext.spec.js +++ b/test/lib/stack-client/stack-client.getcontext.spec.js @@ -1,5 +1,5 @@ import stack from 'lib/stack-client' - +import CozyClient from 'cozy-client' import internal from 'lib/stack-internal' import { @@ -46,9 +46,9 @@ describe("stack client", () => { fetch: jest.fn() } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.getstoragedata.spec.js b/test/lib/stack-client/stack-client.getstoragedata.spec.js index 321cf24a0..1088916c1 100644 --- a/test/lib/stack-client/stack-client.getstoragedata.spec.js +++ b/test/lib/stack-client/stack-client.getstoragedata.spec.js @@ -1,5 +1,5 @@ import stack from 'lib/stack-client' - +import CozyClient from 'cozy-client' import internal from 'lib/stack-internal' describe("stack client", () => { @@ -47,9 +47,9 @@ describe("stack client", () => { fetch: jest.fn() } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.init.spec.js b/test/lib/stack-client/stack-client.init.spec.js index 474157678..88be8f0da 100644 --- a/test/lib/stack-client/stack-client.init.spec.js +++ b/test/lib/stack-client/stack-client.init.spec.js @@ -1,5 +1,5 @@ import stack from 'lib/stack-client' - +import CozyClient from 'cozy-client' import internal from 'lib/stack-internal' import initializeRealtime from 'lib/realtime' @@ -10,14 +10,12 @@ const { init } = stack describe('stack client', () => { describe('init', () => { - let cozyClient = { - getStackClient: () => { - return { - token: { token: 'mytoken' }, - uri: 'https://test.mycozy.cloud' - } + let cozyClient = new CozyClient({ + stackClient: { + token: { token: 'mytoken' }, + uri: 'https://test.mycozy.cloud' } - } + }) let params = { cozyClient, onCreate: function() {}, diff --git a/test/lib/stack-client/stack-client.intents.spec.js b/test/lib/stack-client/stack-client.intents.spec.js index f3b2efc25..ebf5190b2 100644 --- a/test/lib/stack-client/stack-client.intents.spec.js +++ b/test/lib/stack-client/stack-client.intents.spec.js @@ -1,5 +1,6 @@ import { Intents } from 'cozy-interapp' import stack from 'lib/stack-client' +import CozyClient from 'cozy-client' describe('stack client', () => { describe('intents', () => { @@ -8,9 +9,9 @@ describe('stack client', () => { uri: 'https://test.mycozy.cloud' } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient, diff --git a/test/lib/stack-client/stack-client.logout.spec.js b/test/lib/stack-client/stack-client.logout.spec.js index f0b055279..f9a66b2b3 100644 --- a/test/lib/stack-client/stack-client.logout.spec.js +++ b/test/lib/stack-client/stack-client.logout.spec.js @@ -1,5 +1,5 @@ import stack from 'lib/stack-client' - +import CozyClient from 'cozy-client' import internal from 'lib/stack-internal' describe("stack client", () => { @@ -12,9 +12,9 @@ describe("stack client", () => { fetch: jest.fn().mockResolvedValue({status: 200}) } - const cozyClient = { - getStackClient: () => stackClient - } + const cozyClient = new CozyClient({ + stackClient + }) const params = { cozyClient,