Skip to content

Commit

Permalink
Merge pull request #266 from whitedogio/feature/es6-modules-for-fdc3-api
Browse files Browse the repository at this point in the history
Export DesktopAgent methods for ES6 usage
  • Loading branch information
rikoe authored Oct 20, 2020
2 parents ea58aa4 + 0e2338a commit d9a1a44
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store
node_modules
dist
coverage

lib/core/metadata.js
lib/core/MetadataBlog.js
Expand Down
76 changes: 76 additions & 0 deletions src/api/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
AppIntent,
Channel,
Context,
ContextHandler,
IntentResolution,
Listener,
} from '..';

export const open: (name: string, context?: Context) => Promise<void> = (
name,
context
) => {
return window.fdc3.open(name, context);
};

export const findIntent: (
intent: string,
context?: Context
) => Promise<AppIntent> = (intent, context) => {
return window.fdc3.findIntent(intent, context);
};

export const findIntentsByContext: (
context: Context
) => Promise<Array<AppIntent>> = context => {
return window.fdc3.findIntentsByContext(context);
};

export const broadcast: (context: Context) => void = context => {
window.fdc3.broadcast(context);
};

export const raiseIntent: (
intent: string,
context: Context,
target?: string
) => Promise<IntentResolution> = (intent, context, target) => {
return window.fdc3.raiseIntent(intent, context, target);
};

export const addIntentListener: (
intent: string,
handler: ContextHandler
) => Listener = (intent, handler) => {
return window.fdc3.addIntentListener(intent, handler);
};

export const addContextListener: (
contextTypeOrHandler: string | ContextHandler,
handler?: ContextHandler
) => Listener = (a, b) => {
if (typeof a !== 'function') {
return window.fdc3.addContextListener(a as string, b as ContextHandler);
} else {
return window.fdc3.addContextListener(a as ContextHandler);
}
};

export const getSystemChannels: () => Promise<Array<Channel>> = () => {
return window.fdc3.getSystemChannels();
};

export const joinChannel: (channelId: string) => Promise<void> = channelId => {
return window.fdc3.joinChannel(channelId);
};

export const getOrCreateChannel: (
channelId: string
) => Promise<Channel> = channelId => {
return window.fdc3.getOrCreateChannel(channelId);
};

export const getCurrentChannel: () => Promise<Channel> = () => {
return window.fdc3.getCurrentChannel();
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export * from './api/Errors';
export * from './api/IntentMetadata';
export * from './api/IntentResolution';
export * from './api/Listener';
export * from './context/ContextTypes';
export * from './api/methods';
export * from './context/ContextType';
export * from './context/ContextTypes';
export * from './intents/Intents';

declare global {
Expand Down
170 changes: 170 additions & 0 deletions test/methods.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import {
addContextListener,
addIntentListener,
broadcast,
ContextHandler,
ContextTypes,
DesktopAgent,
findIntent,
findIntentsByContext,
getCurrentChannel,
getOrCreateChannel,
getSystemChannels,
joinChannel,
open,
raiseIntent,
} from '../src';
import * as methods from '../src/api/methods';

describe('test ES6 module', () => {
const mocks: Map<string, jest.Mock<any, any>> = new Map();
const getMock: (name: string) => jest.Mock<any, any> = name => {
const mock = mocks.get(name);
if (!mock) {
throw new Error('No mock named ' + name);
}
return mock;
};

beforeAll(() => {
const fdc3 = {};

for (const method of Object.keys(methods)) {
const mock = jest.fn();
mocks.set(method, mock);
Object.defineProperty(fdc3, method, { value: mock });
}

window.fdc3 = fdc3 as DesktopAgent;
});

it('open should delegate to window.fdc3.open', () => {
const name = 'MyApp';
const context = {
type: ContextTypes.Contact,
id: { email: 'test@example.com' },
};

open(name, context);

const mock = getMock('open');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([name, context]);
});

it('findIntent should delegate to window.fdc3.findIntent', () => {
const intent = 'ViewChat';
const context = {
type: ContextTypes.Contact,
id: { email: 'test@example.com' },
};

findIntent(intent, context);

const mock = getMock('findIntent');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([intent, context]);
});

it('findIntentsByContext should delegate to window.fdc3.findIntentsByContext', () => {
const context = {
type: ContextTypes.Contact,
id: { email: 'test@example.com' },
};

findIntentsByContext(context);

const mock = getMock('findIntentsByContext');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([context]);
});

it('broadcast should delegate to window.fdc3.broadcast', () => {
const context = {
type: ContextTypes.Contact,
id: { email: 'test@example.com' },
};

broadcast(context);

const mock = getMock('broadcast');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([context]);
});

it('raiseIntent should delegate to window.fdc3.raiseIntent', () => {
const intent = 'ViewChat';
const context = {
type: ContextTypes.Contact,
id: { email: 'test@example.com' },
};
const target = 'MyApp';

raiseIntent(intent, context, target);

const mock = getMock('raiseIntent');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([intent, context, target]);
});

it('addIntentListener should delegate to window.fdc3.addIntentListener', () => {
const intent = 'ViewChat';
const handler: ContextHandler = _ => {};

addIntentListener(intent, handler);

const mock = getMock('addIntentListener');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([intent, handler]);
});

it('addContextListener should delegate to window.fdc3.addContextListener', () => {
const type = 'fdc3.instrument';
const handler1: ContextHandler = _ => {};
const handler2: ContextHandler = _ => {};

addContextListener(type, handler1);
addContextListener(handler2);

const mock = getMock('addContextListener');
expect(mock.mock.calls.length).toBe(2);
expect(mock.mock.calls[0]).toEqual([type, handler1]);
expect(mock.mock.calls[1]).toEqual([handler2]);
});

it('getSystemChannels should delegate to window.fdc3.getSystemChannels', () => {
getSystemChannels();

const mock = getMock('getSystemChannels');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([]);
});

it('joinChannel should delegate to window.fdc3.joinChannel', () => {
const channelId = 'channel';

joinChannel(channelId);

const mock = getMock('joinChannel');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([channelId]);
});

it('getOrCreateChannel should delegate to window.fdc3.getOrCreateChannel', () => {
const channelId = 'channel';

getOrCreateChannel(channelId);

const mock = getMock('getOrCreateChannel');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([channelId]);
});

it('getCurrentChannel should delegate to window.fdc3.getCurrentChannel', () => {
getCurrentChannel();

const mock = getMock('getCurrentChannel');
expect(mock.mock.calls.length).toBe(1);
expect(mock.mock.calls[0]).toEqual([]);
});
});

0 comments on commit d9a1a44

Please sign in to comment.