Skip to content

Commit

Permalink
Merge branch 'master' into updatelgformatURL
Browse files Browse the repository at this point in the history
  • Loading branch information
cwhitten authored Jun 4, 2020
2 parents 96c0157 + 6498bdb commit 8e0edc3
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { hooks } from '@bfc/test-utils';

import { useStoreContext } from '../../../src/hooks/useStoreContext';
import useNotifications from '../../../src/pages/notifications/useNotifications';

const { renderHook } = hooks;

jest.mock('../../../src/hooks/useStoreContext', () => ({
useStoreContext: jest.fn(),
}));

const state = {
projectId: 'test',
dialogs: [
{
id: 'test',
content: 'test',
luFile: 'test',
referredLuIntents: [],
diagnostics: [
{
message: 'must be an expression',
path: 'test.triggers[0]#Microsoft.OnUnknownIntent#condition',
severity: 1,
source: 'test',
},
],
},
],
luFiles: [
{
content: 'test',
id: 'test.en-us',
intents: [
{
Body: '- test12345 ss',
Entities: [],
Name: 'test',
range: {
endLineNumber: 7,
startLineNumber: 4,
},
},
],
diagnostics: [
{
message: 'lu syntax error',
severity: 0,
source: 'test.en-us',
range: {
end: { character: 2, line: 7 },
start: { character: 0, line: 7 },
},
},
],
},
],
lgFiles: [
{
content: 'test',
id: 'test.en-us',
templates: [
{
body: '- ${add(1,2)}',
name: 'bar',
range: { endLineNumber: 0, startLineNumber: 0 },
},
],
diagnostics: [
{
message: 'lg syntax error',
severity: 1,
source: 'test.en-us',
range: {
end: { character: 2, line: 13 },
start: { character: 0, line: 13 },
},
},
],
},
],
diagnostics: [
{
message: 'server error',
severity: 0,
source: 'server',
},
],
};

const resolvers = { luFileResolver: jest.fn((id) => state.luFiles.find((file) => file.id === id)) };

const actions = {
updateLuFile: jest.fn(),
};

(useStoreContext as jest.Mock).mockReturnValue({
state,
resolvers,
actions,
});

describe('useNotification hooks', () => {
it('should return notifications', () => {
const { result } = renderHook(() => useNotifications());

expect(result.current.length).toBe(4);
});

it('should return filtered notifications', () => {
const { result } = renderHook(() => useNotifications('Error'));

expect(result.current.length).toBe(2);
});
});
139 changes: 139 additions & 0 deletions Composer/packages/client/__tests__/shell/lgApi.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { hooks } from '@bfc/test-utils';

import { useLgApi } from '../../src/shell/lgApi';
import { useStoreContext } from '../../src/hooks/useStoreContext';

jest.mock('../../src/hooks/useStoreContext', () => ({
useStoreContext: jest.fn(),
}));

const { renderHook } = hooks;

const state = {
lgFiles: [
{
content: 'test',
id: 'test.en-us',
templates: [
{
body: '- ${add(1,2)}',
name: 'bar',
},
],
},
],
focusPath: '',
locale: 'en-us',
projectId: 'test',
};

const resolvers = { lgFileResolver: jest.fn((id) => state.lgFiles.find((file) => file.id === id)) };

const actions = {
updateLgTemplate: jest.fn(),
copyLgTemplate: jest.fn(),
removeLgTemplate: jest.fn(),
removeLgTemplates: jest.fn(),
};

(useStoreContext as jest.Mock).mockReturnValue({
state,
resolvers,
actions,
});

describe('use lgApi hooks', () => {
it('should get lg template by id', () => {
const { result } = renderHook(() => useLgApi());

const templates = result.current.getLgTemplates('test.en-us');
expect(templates[0].name).toBe('bar');

function testWrongId() {
result.current.getLgTemplates('wrong.en-us');
}
expect(testWrongId).toThrow();
});

it('should call update lg template action', () => {
const { result } = renderHook(() => useLgApi());

result.current.updateLgTemplate('test.en-us', 'bar', 'update');

expect(actions.updateLgTemplate).toBeCalledTimes(1);
const arg = {
file: {
content: 'test',
id: 'test.en-us',
templates: [{ body: '- ${add(1,2)}', name: 'bar' }],
},
projectId: 'test',
template: {
body: 'update',
name: 'bar',
parameters: [],
},
templateName: 'bar',
};
expect(actions.updateLgTemplate).toBeCalledWith(arg);
});

it('should call copy lg template action', () => {
const { result } = renderHook(() => useLgApi());

result.current.copyLgTemplate('test.en-us', 'from', 'to');

expect(actions.copyLgTemplate).toBeCalledTimes(1);
const arg = {
file: {
content: 'test',
id: 'test.en-us',
templates: [{ body: '- ${add(1,2)}', name: 'bar' }],
},
fromTemplateName: 'from',
projectId: 'test',
toTemplateName: 'to',
};
expect(actions.copyLgTemplate).toBeCalledWith(arg);
});

it('should call remove lg template action', () => {
const { result } = renderHook(() => useLgApi());

result.current.removeLgTemplate('test.en-us', 'bar');

expect(actions.removeLgTemplate).toBeCalledTimes(1);

const arg = {
file: {
content: 'test',
id: 'test.en-us',
templates: [{ body: '- ${add(1,2)}', name: 'bar' }],
},
projectId: 'test',
templateName: 'bar',
};
expect(actions.removeLgTemplate).toBeCalledWith(arg);
});

it('should call remove lg templates action', () => {
const { result } = renderHook(() => useLgApi());

result.current.removeLgTemplates('test.en-us', ['bar']);

expect(actions.removeLgTemplates).toBeCalledTimes(1);
const arg = {
file: {
content: 'test',
id: 'test.en-us',
templates: [{ body: '- ${add(1,2)}', name: 'bar' }],
},
projectId: 'test',
templateNames: ['bar'],
};
expect(actions.removeLgTemplates).toBeCalledWith(arg);
});
});
90 changes: 90 additions & 0 deletions Composer/packages/client/__tests__/shell/luApi.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { hooks } from '@bfc/test-utils';

import { useLuApi } from '../../src/shell/luApi';
import { useStoreContext } from '../../src/hooks/useStoreContext';

const { renderHook } = hooks;

jest.mock('../../src/hooks/useStoreContext', () => ({
useStoreContext: jest.fn(),
}));

jest.mock('../../src/store/parsers/luWorker', () => {
return { addIntent: (a, b) => b.Body, updateIntent: (a, b, c) => c.Body, removeIntent: (a, b) => b };
});

const state = {
luFiles: [
{
content: 'test',
id: 'test.en-us',
intents: [{ Body: '- test12345', Entities: [], Name: 'test' }],
},
],
focusPath: '',
locale: 'en-us',
projectId: 'test',
};

const resolvers = { luFileResolver: jest.fn((id) => state.luFiles.find((file) => file.id === id)) };

const actions = {
updateLuFile: jest.fn(),
};

(useStoreContext as jest.Mock).mockReturnValue({
state,
resolvers,
actions,
});

describe('use luApi hooks', () => {
it('should call add lu intent action', () => {
const { result } = renderHook(() => useLuApi());

result.current.addLuIntent('test.en-us', 'test', { Body: '- test add', Name: 'add' }).then(() => {
expect(actions.updateLuFile).toBeCalledTimes(1);
const arg = { content: '- test add', id: 'test.en-us', projectId: 'test' };
expect(actions.updateLuFile).toBeCalledWith(arg);
});
});

it('should call update lu intent action', () => {
const { result } = renderHook(() => useLuApi());

result.current.updateLuIntent('test.en-us', 'test', { Body: '- test update', Name: 'update' }).then(() => {
expect(actions.updateLuFile).toBeCalledTimes(2);
const arg = { content: '- test update', id: 'test.en-us', projectId: 'test' };
expect(actions.updateLuFile).toBeCalledWith(arg);
});
});

it('should call remove lu intent action', () => {
const { result } = renderHook(() => useLuApi());

result.current.removeLuIntent('test.en-us', 'remove').then(() => {
expect(actions.updateLuFile).toBeCalledTimes(3);
const arg = { content: 'remove', id: 'test.en-us', projectId: 'test' };
expect(actions.updateLuFile).toBeCalledWith(arg);
});
});

it('should get lu intents', () => {
const { result } = renderHook(() => useLuApi());

const intents = result.current.getLuIntents('test.en-us');

expect(intents[0].Name).toBe('test');
});

it('should get lu intent', () => {
const { result } = renderHook(() => useLuApi());

const intent = result.current.getLuIntent('test.en-us', 'test');

expect(intent?.Name).toBe('test');
});
});
12 changes: 12 additions & 0 deletions Composer/packages/client/src/hooks/useStoreContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { useContext } from 'react';

import { StoreContext } from '../store';

export function useStoreContext() {
const { state, actions, resolvers } = useContext(StoreContext);

return { state, actions, resolvers };
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { useContext, useMemo } from 'react';
import { useMemo } from 'react';

import { StoreContext } from '../../store';
import { useStoreContext } from '../../hooks/useStoreContext';

import { Notification, DialogNotification, LuNotification, LgNotification, ServerNotification } from './types';
import { getReferredFiles } from './../../utils/luUtil';
export default function useNotifications(filter?: string) {
const { state } = useContext(StoreContext);
const { state } = useStoreContext();
const { dialogs, luFiles, lgFiles, projectId, diagnostics } = state;
const memoized = useMemo(() => {
const notifactions: Notification[] = [];
Expand Down
Loading

0 comments on commit 8e0edc3

Please sign in to comment.