Skip to content

Commit

Permalink
chore: add schema diff UT & add null checking in deleteTrigger (#2856)
Browse files Browse the repository at this point in the history
* null checking in deleteTrigger

* add UT for getCustomSchema

* fix ts type

Co-authored-by: Ben Yackley <61990921+beyackle@users.noreply.github.com>
Co-authored-by: Andy Brown <asbrown002@gmail.com>
Co-authored-by: Chris Whitten <christopher.whitten@microsoft.com>
  • Loading branch information
4 people authored May 8, 2020
1 parent c9496ed commit 9d221cf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
16 changes: 13 additions & 3 deletions Composer/packages/client/src/utils/dialogUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { ConceptLabels, DialogGroup, SDKKinds, dialogGroups, DialogInfo, DialogFactory, ITrigger } from '@bfc/shared';
import {
ConceptLabels,
DialogGroup,
SDKKinds,
dialogGroups,
DialogInfo,
DialogFactory,
ITriggerCondition,
} from '@bfc/shared';
import get from 'lodash/get';
import set from 'lodash/set';
import cloneDeep from 'lodash/cloneDeep';
Expand Down Expand Up @@ -138,7 +146,7 @@ export function deleteTrigger(
dialogs: DialogInfo[],
dialogId: string,
index: number,
callbackOnDeletedTrigger?: (trigger: ITrigger) => any
callbackOnDeletedTrigger?: (trigger: ITriggerCondition) => any
) {
let dialogCopy = getDialog(dialogs, dialogId);
if (!dialogCopy) return null;
Expand All @@ -149,7 +157,9 @@ export function deleteTrigger(
}
const triggers = get(dialogCopy, 'content.triggers');
const removedTriggers = triggers.splice(index, 1);
callbackOnDeletedTrigger && callbackOnDeletedTrigger(removedTriggers[0]);
if (callbackOnDeletedTrigger && removedTriggers[0]) {
callbackOnDeletedTrigger(removedTriggers[0]);
}
return dialogCopy.content;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { ShellApi, SDKKinds } from '@bfc/shared';
import { ShellApi, SDKKinds, ITriggerCondition, BaseSchema } from '@bfc/shared';
import get from 'lodash/get';

import { useActionApi } from './useActionApi';
Expand All @@ -11,15 +11,17 @@ export const useTriggerApi = (shellAPi: ShellApi) => {
const { deleteActions } = useActionApi(shellAPi);
const { deleteLuIntent } = useLuApi(shellAPi);

const deleteTrigger = (dialogId: string, trigger) => {
const deleteTrigger = (dialogId: string, trigger: ITriggerCondition) => {
if (!trigger) return;

// Clean the lu resource on intent trigger
if (trigger.$kind === SDKKinds.OnIntent) {
if (get(trigger, '$kind') === SDKKinds.OnIntent) {
const triggerIntent = get(trigger, 'intent', '');
deleteLuIntent(dialogId, triggerIntent);
}

// Clean action resources
const actions = get(trigger, 'actions');
const actions = get(trigger, 'actions') as BaseSchema[];
if (!actions || !Array.isArray(actions)) return;

deleteActions(dialogId, actions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { OBISchema } from '@bfc/shared';

import { getCustomSchema } from '../../src/utils/getCustomSchema';

describe('getCustomSchema', () => {
it('can handle invalid input', () => {
expect(getCustomSchema()).toBeUndefined();
expect(getCustomSchema({}, undefined)).toBeUndefined();
expect(getCustomSchema(undefined, undefined)).toBeUndefined();

expect(getCustomSchema({}, {})).toBeUndefined();
});

it('can genreate diff schema', () => {
const ejected = {
definitions: {
'Microsoft.SendActivity': {
title: 'SendActivity Title',
description: 'Send an activity.',
},
},
} as OBISchema;
expect(getCustomSchema({ oneOf: [], definitions: {} }, ejected)).toEqual({
oneOf: [
{
title: 'SendActivity Title',
description: 'Send an activity.',
$ref: '#/definitions/Microsoft.SendActivity',
},
],
definitions: {
'Microsoft.SendActivity': ejected.definitions['Microsoft.SendActivity'],
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OBISchema } from '@bfc/shared';

export const getCustomSchema = (baseSchema?: OBISchema, ejectedSchema?: OBISchema): OBISchema | undefined => {
if (!baseSchema || !ejectedSchema) return;
if (typeof baseSchema.definitions !== 'object' || typeof ejectedSchema.definitions !== 'object') return;

const baseDefinitions = baseSchema.definitions;
const baseKindHash = Object.keys(baseDefinitions).reduce((hash, $kind) => {
Expand Down

0 comments on commit 9d221cf

Please sign in to comment.