Skip to content

Commit

Permalink
Merge pull request #2 from tef-novum/pladaria
Browse files Browse the repository at this point in the history
complete test coverage
  • Loading branch information
pladaria authored Nov 29, 2018
2 parents a2a2a6c + f4517e9 commit fb97c33
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 21 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ if (isWebViewBridgeAvailable()) {

Show native picker UI in order to let the user select a contact.

Picker UI elements can be filtered by available phones (default) or emails
- Android only: picker UI elements can be filtered by available phones
(default) or emails. `filter` property is ignored by iOS devices

<img height="550" src="doc/webview-bridge-contact-ios.png"><img height="550" src="doc/webview-bridge-contact-android.png">

Expand Down Expand Up @@ -163,7 +164,7 @@ Update webview title. If the bridge is not present, automatically fallbacks to a
`document.title` update.
```typescript
export declare const setWebViewTitle: (title: string) => Promise<void>;
setWebViewTitle: (title: string) => Promise<void>;
```
#### Example
Expand All @@ -184,7 +185,7 @@ browser confirm.
<img height="550" src="doc/webview-bridge-confirm-ios.png"><img height="550" src="doc/webview-bridge-confirm-android.png">
```typescript
export declare const nativeConfirm: (
nativeConfirm: (
{
message,
title,
Expand Down Expand Up @@ -226,7 +227,7 @@ browser alert.
<img height="550" src="doc/webview-bridge-alert-ios.png"><img height="550" src="doc/webview-bridge-alert-android.png">
```typescript
export declare const nativeAlert: (
nativeAlert: (
{
message,
title,
Expand Down Expand Up @@ -264,7 +265,7 @@ browser alert.
<img height="550" src="doc/webview-bridge-message-ios.png"><img height="550" src="doc/webview-bridge-message-android.png">
```typescript
export declare const nativeMessage: (
nativeMessage: (
{
message,
duration,
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = {
moduleFileExtensions: ['js', 'ts', 'tsx'],
transform: {
'^.+\\.(ts|tsx|js)$': 'ts-jest',
'^.+\\.ts$': 'ts-jest',
},
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
},
},
testMatch: ['**/__tests__/*.+(ts|tsx|js)'],
testMatch: ['**/__tests__/*-test.ts'],
preset: 'ts-jest/presets/js-with-ts',
collectCoverage: true,
collectCoverageFrom: ['**/src/*.{ts}', '!**/__tests__/**', '!**/dist/**'],
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/calendar-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test('set a calendar event', async cb => {
title: 'some title',
}).then(res => {
expect(res).toBeUndefined();
delete window.tuentiWebView;
cb();
});
});
60 changes: 60 additions & 0 deletions src/__tests__/contacts-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {requestContact} from '../contacts';
import {
createFakeAndroidPostMessage,
removeFakeAndroidPostMessage,
} from './fake-post-message';

const ANY_CONTACT = {
name: 'Homer J. Simpson',
email: 'chunkylover53@aol.com',
phoneNumber: '(939)-555-0113',
address: {
street: '742 Evergreen Terrace',
city: 'Springfield',
country: 'USA',
postalCode: '49007',
},
};

test('request contact', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('GET_CONTACT_DATA');
},
getResponse: message => ({
type: message.type,
id: message.id,
payload: ANY_CONTACT,
}),
});

requestContact().then(res => {
expect(res).toEqual(ANY_CONTACT);
removeFakeAndroidPostMessage();
cb();
});
});

test('request contact filtered', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('GET_CONTACT_DATA');
expect(message.payload).toEqual({
filter: 'email',
});
},
getResponse: message => ({
type: message.type,
id: message.id,
payload: ANY_CONTACT,
}),
});

requestContact({
filter: 'email',
}).then(res => {
expect(res).toEqual(ANY_CONTACT);
removeFakeAndroidPostMessage();
cb();
});
});
115 changes: 115 additions & 0 deletions src/__tests__/device-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {requestSimIcc, requestSimImsi, requestDeviceImei} from '../device';
import {
createFakeAndroidPostMessage,
removeFakeAndroidPostMessage,
} from './fake-post-message';

const ANY_STRING = 'any-string';

test('request sim icc', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('SIM_ICC');
expect(message.payload).toBeUndefined();
},
getResponse: message => ({
type: message.type,
id: message.id,
payload: {icc: ANY_STRING},
}),
});

requestSimIcc().then(res => {
expect(res).toEqual(ANY_STRING);
removeFakeAndroidPostMessage();
cb();
});
});

test('request sim imsi', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('IMSI');
expect(message.payload).toBeUndefined();
},
getResponse: message => ({
type: message.type,
id: message.id,
payload: {imsi: ANY_STRING},
}),
});

requestSimImsi().then(res => {
expect(res).toEqual(ANY_STRING);
removeFakeAndroidPostMessage();
cb();
});
});

test('request device imei', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('IMEI');
expect(message.payload).toBeUndefined();
},
getResponse: message => ({
type: message.type,
id: message.id,
payload: {imei: ANY_STRING},
}),
});

requestDeviceImei().then(res => {
expect(res).toEqual(ANY_STRING);
removeFakeAndroidPostMessage();
cb();
});
});

test('request sim icc (failed)', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('SIM_ICC');
expect(message.payload).toBeUndefined();
},
getResponse: () => ({type: 'other', id: ''}),
});

requestSimIcc().then(res => {
expect(res).toBeNull();
removeFakeAndroidPostMessage();
cb();
});
});

test('request sim imsi (failed)', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('IMSI');
expect(message.payload).toBeUndefined();
},
getResponse: () => ({type: 'other', id: ''}),
});

requestSimImsi().then(res => {
expect(res).toBeNull();
removeFakeAndroidPostMessage();
cb();
});
});

test('request device imei (failed)', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('IMEI');
expect(message.payload).toBeUndefined();
},
getResponse: () => ({type: 'other', id: ''}),
});

requestDeviceImei().then(res => {
expect(res).toBeNull();
removeFakeAndroidPostMessage();
cb();
});
});
146 changes: 146 additions & 0 deletions src/__tests__/dialogs-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import {nativeAlert, nativeConfirm, nativeMessage} from '../dialogs';
import {
createFakeAndroidPostMessage,
removeFakeAndroidPostMessage,
} from './fake-post-message';

const ANY_TITLE = 'any-title';
const ANY_MESSAGE = 'any-message';
const ANY_BUTTON_TEXT = 'any-button-text';
const ANY_ACCEPT_TEXT = 'any-accept-text';
const ANY_CANCEL_TEXT = 'any-cancel-text';
const ANY_DURATION = 5000;

afterEach(() => {
removeFakeAndroidPostMessage();
});

test('native alert', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('ALERT');
expect(message.payload!).toEqual({
title: ANY_TITLE,
message: ANY_MESSAGE,
buttonText: ANY_BUTTON_TEXT,
});
},
getResponse: message => ({
type: message.type,
id: message.id,
}),
});

nativeAlert({
title: ANY_TITLE,
message: ANY_MESSAGE,
buttonText: ANY_BUTTON_TEXT,
}).then(res => {
expect(res).toBeUndefined();
cb();
});
});

test('native confirm', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('CONFIRM');
expect(message.payload).toEqual({
message: ANY_MESSAGE,
title: ANY_TITLE,
acceptText: ANY_ACCEPT_TEXT,
cancelText: ANY_CANCEL_TEXT,
});
},
getResponse: message => ({
type: message.type,
id: message.id,
payload: {result: true},
}),
});

nativeConfirm({
title: ANY_TITLE,
message: ANY_MESSAGE,
acceptText: ANY_ACCEPT_TEXT,
cancelText: ANY_CANCEL_TEXT,
}).then(res => {
expect(res).toBe(true);
cb();
});
});

test('native message', async cb => {
createFakeAndroidPostMessage({
checkMessage: message => {
expect(message.type).toBe('MESSAGE');
expect(message.payload).toEqual({
message: ANY_MESSAGE,
duration: ANY_DURATION,
buttonText: ANY_BUTTON_TEXT,
});
},
getResponse: message => ({
type: message.type,
id: message.id,
}),
});

nativeMessage({
message: ANY_MESSAGE,
duration: ANY_DURATION,
buttonText: ANY_BUTTON_TEXT,
}).then(res => {
expect(res).toBeUndefined();
cb();
});
});

test('native alert fallbacks to browser alert', async cb => {
const alert = jest.fn();
window.alert = alert;

nativeAlert({
message: ANY_MESSAGE,
}).then(res => {
expect(res).toBeUndefined();
expect(alert.mock.calls.length).toBe(1);
expect(alert.mock.calls[0][0]).toBe(ANY_MESSAGE);
cb();
});

delete window.alert;
});

test('native confirm fallbacks to browser confirm', async cb => {
const confirm = jest.fn().mockReturnValue(true);

window.confirm = confirm;

nativeConfirm({
message: ANY_MESSAGE,
}).then(res => {
expect(res).toBe(true);
expect(confirm.mock.calls.length).toBe(1);
expect(confirm.mock.calls[0][0]).toBe(ANY_MESSAGE);
cb();
});

delete window.confirm;
});

test('native message fallbacks to browser alert', async cb => {
const alert = jest.fn();
window.alert = alert;

nativeMessage({
message: ANY_MESSAGE,
}).then(res => {
expect(res).toBeUndefined();
expect(alert.mock.calls.length).toBe(1);
expect(alert.mock.calls[0][0]).toBe(ANY_MESSAGE);
cb();
});

delete window.alert;
});
Loading

0 comments on commit fb97c33

Please sign in to comment.