Add jest testing
ADD
jest.config.js
+module.exports = {
+ moduleNameMapper: {
+ '^cordova/(.*)$': '<rootDir>/node_modules/cordova-js/src/common/$1',
+ },
+ globals: {
+ cordova: {},
+ },
+};

MODIFY
package.json
package.json
}
],
"scripts": {
- "test": "npm run eslint",
+ "test": "jest && npm run eslint",
+ "test:watch": "jest --watch",
"eslint": "node node_modules/eslint/bin/eslint www && node node_modules/eslint/bin/eslint src",
"test:android": "cordova-paramedic --cleanUpAfterRun --verbose --platform android --plugin '. --variable API_KEY_FOR_ANDROID=test' --useTunnel",
"test:browser": "cordova-paramedic --cleanUpAfterRun --verbose --platform browser --plugin '. --variable API_KEY_FOR_ANDROID=test' --useTunnel"
},
"homepage": "https://github.com/mapsplugin/cordova-plugin-googlemaps",
"devDependencies": {
- "eslint": "^5.7.0"
+ "cordova-js": "^4.2.4",
+ "eslint": "^5.7.0",
+ "jest": "^23.6.0"
}
}

ADD
www/Geocode.spec.js
+const Geocoder = require('./Geocoder');
+
+describe('Geocode', () => {
+ // Initialize them here so that vscode typing
+ // will work without this being a TS project
+ let geocoder = new Geocoder();
+ let execSpy = jest.fn();
+
+ beforeEach(() => {
+ execSpy = jest.fn();
+ geocoder = new Geocoder(execSpy);
+ });
+
+ it('should exist', () => {
+ expect(geocoder).toBeDefined();
+ });
+
+ describe('geocode()', () => {
+ it('should exist', () => {
+ expect(geocoder.geocode).toBeDefined();
+ });
+
+ fit('should return a promise if a callback is not passed', async () => {
+ const request = {
+ address: '',
+ };
+ const mockResults = [
+ {
+ foo: 'bar',
+ },
+ ];
+ execSpy.mockImplementation(resolve => {
+ resolve({
+ results: mockResults,
+ });
+ });
+
+ const results = geocoder.geocode(request);
+ expect(results).toMatchSnapshot();
+ await expect(results).resolves.toMatchSnapshot();
+ });
+
+ it('should error if both position and address options are provided', async () => {
+ const request = {
+ position: [],
+ address: [],
+ };
+
+ await expect(geocoder.geocode(request)).rejects.toMatchSnapshot();
+ });
+
+ it('should call the if both position and address options are provided', () => {
+ const request = {
+ position: [],
+ address: [],
+ };
+
+ expect(geocoder.geocode(request)).rejects.toMatchSnapshot();
+ });
+
+ describe('single request', () => {
+ describe('position', () => {
+ it('should call the native plugin with the correct parameters', () => {
+ geocoder.geocode({
+ position: {
+ lat: 0,
+ lng: 0,
+ },
+ });
+ expect(execSpy.mock.calls).toMatchSnapshot();
+ });
+ });
+
+ describe('address', () => {
+ it('should call the native plugin with the correct parameters', () => {
+ geocoder.geocode({
+ address: 'Tokyo, Japan',
+ });
+ expect(execSpy.mock.calls).toMatchSnapshot();
+ });
+ });
+ });
+
+ xdescribe('multiple requests', () => {
+ // todo
+ });
+ });
+});

ADD
www/__snapshots__/Geocode.spec.js.snap
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Geocode geocode() should call the if both position and address options are provided 1`] = `[Error: Invalid request]`;
+
+exports[`Geocode geocode() should error if both position and address options are provided 1`] = `[Error: Invalid request]`;
+
+exports[`Geocode geocode() should return a promise if a callback is not passed 1`] = `Promise {}`;
+
+exports[`Geocode geocode() should return a promise if a callback is not passed 2`] = `
+Array [
+ Object {
+ "foo": "bar",
+ },
+]
+`;
+
+exports[`Geocode geocode() single request address should call the native plugin with the correct parameters 1`] = `
+Array [
+ Array [
+ [Function],
+ [Function],
+ "PluginGeocoder",
+ "geocode",
+ Array [
+ Object {
+ "address": "Tokyo, Japan",
+ "idx": -1,
+ },
+ ],
+ ],
+]
+`;
+
+exports[`Geocode geocode() single request position should call the native plugin with the correct parameters 1`] = `
+Array [
+ Array [
+ [Function],
+ [Function],
+ "PluginGeocoder",
+ "geocode",
+ Array [
+ Object {
+ "idx": -1,
+ "position": Object {
+ "lat": 0,
+ "lng": 0,
+ },
+ },
+ ],
+ ],
+]
+`;

Add jest testing