diff --git a/src/__tests__/calendar-test.ts b/src/__tests__/calendar-test.ts index 8edc0a0..ab2b7bd 100644 --- a/src/__tests__/calendar-test.ts +++ b/src/__tests__/calendar-test.ts @@ -1,4 +1,8 @@ import {createCalendarEvent} from '../calendar'; +import { + createFakeAndroidPostMessage, + removeFakeAndroidPostMessage, +} from './fake-post-message'; test('set a calendar event', async cb => { expect.assertions(4); @@ -6,22 +10,17 @@ test('set a calendar event', async cb => { const t1 = 1000; const t2 = 2000; - window.tuentiWebView = { - postMessage: jsonMessage => { - const message = JSON.parse(jsonMessage); - - expect(message.type).toBe('CREATE_CALENDAR_EVENT'); - expect(message.payload.beginTime).toBe(1); - expect(message.payload.endTime).toBe(2); - - window.__tuenti_webview_bridge!.postMessage( - JSON.stringify({ - type: 'CREATE_CALENDAR_EVENT', - id: message.id, - }), - ); + createFakeAndroidPostMessage({ + checkMessage: msg => { + expect(msg.type).toBe('CREATE_CALENDAR_EVENT'); + expect(msg.payload.beginTime).toBe(1); + expect(msg.payload.endTime).toBe(2); }, - }; + getResponse: msg => ({ + type: 'CREATE_CALENDAR_EVENT', + id: msg.id, + }), + }); createCalendarEvent({ beginTime: t1, @@ -29,7 +28,7 @@ test('set a calendar event', async cb => { title: 'some title', }).then(res => { expect(res).toBeUndefined(); - delete window.tuentiWebView; + removeFakeAndroidPostMessage(); cb(); }); }); diff --git a/src/__tests__/contacts-test.ts b/src/__tests__/contacts-test.ts index 62c920d..4546943 100644 --- a/src/__tests__/contacts-test.ts +++ b/src/__tests__/contacts-test.ts @@ -16,6 +16,10 @@ const ANY_CONTACT = { }, }; +afterEach(() => { + removeFakeAndroidPostMessage(); +}); + test('request contact', async cb => { createFakeAndroidPostMessage({ checkMessage: message => { @@ -30,7 +34,6 @@ test('request contact', async cb => { requestContact().then(res => { expect(res).toEqual(ANY_CONTACT); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -54,7 +57,6 @@ test('request contact filtered', async cb => { filter: 'email', }).then(res => { expect(res).toEqual(ANY_CONTACT); - removeFakeAndroidPostMessage(); cb(); }); }); diff --git a/src/__tests__/device-test.ts b/src/__tests__/device-test.ts index d1f3ba7..990883a 100644 --- a/src/__tests__/device-test.ts +++ b/src/__tests__/device-test.ts @@ -6,6 +6,10 @@ import { const ANY_STRING = 'any-string'; +afterEach(() => { + removeFakeAndroidPostMessage(); +}); + test('request sim icc', async cb => { createFakeAndroidPostMessage({ checkMessage: message => { @@ -21,7 +25,6 @@ test('request sim icc', async cb => { requestSimIcc().then(res => { expect(res).toEqual(ANY_STRING); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -41,7 +44,6 @@ test('request sim imsi', async cb => { requestSimImsi().then(res => { expect(res).toEqual(ANY_STRING); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -61,7 +63,6 @@ test('request device imei', async cb => { requestDeviceImei().then(res => { expect(res).toEqual(ANY_STRING); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -77,7 +78,6 @@ test('request sim icc (failed)', async cb => { requestSimIcc().then(res => { expect(res).toBeNull(); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -93,7 +93,6 @@ test('request sim imsi (failed)', async cb => { requestSimImsi().then(res => { expect(res).toBeNull(); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -109,7 +108,6 @@ test('request device imei (failed)', async cb => { requestDeviceImei().then(res => { expect(res).toBeNull(); - removeFakeAndroidPostMessage(); cb(); }); }); diff --git a/src/__tests__/utils-test.ts b/src/__tests__/utils-test.ts index 2400c3a..1bc2303 100644 --- a/src/__tests__/utils-test.ts +++ b/src/__tests__/utils-test.ts @@ -6,6 +6,10 @@ import { const ANY_STRING = 'any-string'; +afterEach(() => { + removeFakeAndroidPostMessage(); +}); + test('attach to email', async cb => { const PARAMS = { url: 'any-url', @@ -28,7 +32,6 @@ test('attach to email', async cb => { attachToEmail(PARAMS).then(res => { expect(res).toBeUndefined(); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -47,7 +50,6 @@ test('set webview title', async cb => { setWebViewTitle(ANY_STRING).then(res => { expect(res).toBeUndefined(); - removeFakeAndroidPostMessage(); cb(); }); }); @@ -76,7 +78,6 @@ test('notify page loaded', async cb => { notifyPageLoaded().then(res => { expect(res).toBeUndefined(); - removeFakeAndroidPostMessage(); cb(); }); });