Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge test refactor #6752

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
202 changes: 113 additions & 89 deletions core/src/tests/bridge.spec.ts
Expand Up @@ -3,40 +3,99 @@
*/

import { initBridge } from '../../native-bridge';
import type {
CapacitorInstance,
PluginResult,
WindowCapacitor,
} from '../definitions-internal';
import { createCapacitor } from '../runtime';

describe('bridge', () => {
import type { PluginResult, WindowCapacitor } from '../definitions-internal';

const platforms: [
name: string,
setup: (win: WindowCapacitor) => {
mockPluginResult: (pluginResult: PluginResult) => void;
},
][] = [
[
'ios',
(win: WindowCapacitor) => {
const mockPostMessage = jest.fn();
win.webkit = {
messageHandlers: {
bridge: {
postMessage: mockPostMessage,
},
},
};

return {
mockPluginResult: (pluginResult: PluginResult) => {
mockPostMessage.mockImplementation(m => {
pluginResult.callbackId = m.callbackId;
pluginResult.methodName = m.methodName;

Promise.resolve().then(() =>
win.Capacitor.fromNative(pluginResult),
);
});
},
};
},
],
[
'android',
(win: WindowCapacitor) => {
const mockPostMessage = jest.fn();
win.androidBridge = {
postMessage: mockPostMessage,
};

return {
mockPluginResult: (pluginResult: PluginResult) => {
mockPostMessage.mockImplementation(m => {
const d = JSON.parse(m);
pluginResult.callbackId = d.callbackId;
pluginResult.methodName = d.methodName;

Promise.resolve().then(() =>
win.androidBridge.onmessage({
data: JSON.stringify(pluginResult),
}),
);
});
},
};
},
],
];

describe.each(platforms)('%s bridge', (platformName, setup) => {
let win: WindowCapacitor;
let cap: CapacitorInstance;
let mocks: { mockPluginResult: (pluginResult: PluginResult) => void };

beforeEach(() => {
win = {};
initBridge(win);
mocks = setup(win);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-empty-function
window.prompt = () => {};

initBridge(win);
});

it('getPlatform', () => {
expect(win.Capacitor.getPlatform()).toBe(platformName);
});

it('isNativePlatform', () => {
expect(win.Capacitor.isNativePlatform()).toBe(true);
});

it('android nativePromise error', done => {
mockAndroidPluginResult({
it('nativePromise error', done => {
mocks.mockPluginResult({
success: false,
data: null,
error: { message: 'darn it' },
});
initBridge(win);

cap = createCapacitor(win);
expect(cap.getPlatform()).toBe('android');
expect(cap.isNativePlatform()).toBe(true);

cap
.nativePromise('id', 'method')
win.Capacitor.nativePromise('id', 'method')
.then(() => {
done('should throw error');
})
Expand All @@ -50,19 +109,13 @@ describe('bridge', () => {
});
});

it('android nativePromise success', done => {
mockAndroidPluginResult({
it('nativePromise success', done => {
mocks.mockPluginResult({
success: true,
data: { mph: 88 },
});
initBridge(win);

cap = createCapacitor(win);
expect(cap.getPlatform()).toBe('android');
expect(cap.isNativePlatform()).toBe(true);

cap
.nativePromise('id', 'method')
win.Capacitor.nativePromise('id', 'method')
.then(data => {
try {
expect(data).toEqual({ mph: 88 });
Expand All @@ -74,77 +127,48 @@ describe('bridge', () => {
.catch(done);
});

it('ios nativeCallback w/ callback error', done => {
mockIosPluginResult({
it('nativeCallback w/ callback error', done => {
mocks.mockPluginResult({
data: null,
success: false,
error: { message: 'darn it' },
});
initBridge(win);

cap = createCapacitor(win);
expect(cap.getPlatform()).toBe('ios');
expect(cap.isNativePlatform()).toBe(true);

cap.nativeCallback('pluginName', 'methodName', {}, (data, err) => {
try {
expect(data).toEqual(null);
expect(err.message).toBe('darn it');
done();
} catch (e) {
done(e);
}
});
win.Capacitor.nativeCallback(
'pluginName',
'methodName',
{},
(data, err) => {
try {
expect(data).toEqual(null);
expect(err.message).toBe('darn it');
done();
} catch (e) {
done(e);
}
},
);
});

it('ios nativeCallback w/ options and callback, success', done => {
mockIosPluginResult({
it('nativeCallback w/ options and callback, success', done => {
mocks.mockPluginResult({
data: { mph: 88 },
success: true,
});
initBridge(win);

cap = createCapacitor(win);
expect(cap.getPlatform()).toBe('ios');
expect(cap.isNativePlatform()).toBe(true);

cap.nativeCallback('pluginName', 'methodName', {}, (data, err) => {
try {
expect(data).toEqual({ mph: 88 });
expect(err).toBe(undefined);
done();
} catch (e) {
done(e);
}
});
});

const mockAndroidPluginResult = (pluginResult: PluginResult) => {
win.androidBridge = {
postMessage: m => {
const d = JSON.parse(m);
Promise.resolve().then(() => {
pluginResult.callbackId = d.callbackId;
pluginResult.methodName = d.methodName;
cap.fromNative(pluginResult);
});
},
};
};

const mockIosPluginResult = (pluginResult: PluginResult) => {
win.webkit = {
messageHandlers: {
bridge: {
postMessage: m => {
Promise.resolve().then(() => {
pluginResult.callbackId = m.callbackId;
pluginResult.methodName = m.methodName;
cap.fromNative(pluginResult);
});
},
},
win.Capacitor.nativeCallback(
'pluginName',
'methodName',
{},
(data, err) => {
try {
expect(data).toEqual({ mph: 88 });
expect(err).toBe(undefined);
done();
} catch (e) {
done(e);
}
},
};
};
);
});
});