Skip to content

Commit

Permalink
Fix null renderMarkdown and add tests (#2724)
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim authored Dec 12, 2019
1 parent 7d67f26 commit 7811093
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixes [#2709](https://github.com/microsoft/BotFramework-WebChat/issues/2709). Reduce wasted render of activities by memoizing partial result of `<BasicTranscript>`, by [@compulim](https://github.com/compulim) in PR [#2710](https://github.com/microsoft/BotFramework-WebChat/pull/2710)
- Fixes [#2710](https://github.com/microsoft/BotFramework-WebChat/issues/2710). Suggested actions container should persist for AT, by [@corinagum](https://github.com/corinagum) in PR [#2710](https://github.com/microsoft/BotFramework-WebChat/pull/2710)
- Fixes [#2718](https://github.com/microsoft/BotFramework-WebChat/issues/2718). Add `Object.is` polyfill for IE11, by [@compulim](https://github.com/compulim) in PR [#2719](https://github.com/microsoft/BotFramework-WebChat/pull/2719)
- Fixes [#2723](https://github.com/microsoft/BotFramework-WebChat/issues/2723). Fix `renderMarkdown` should not be called if it is `undefined` in minimal bundle, by [@compulim](https://github.com/compulim) in PR [#2724](https://github.com/microsoft/BotFramework-WebChat/pull/2724)

### Changed

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions __tests__/hooks/useRenderMarkdownAsHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ test('renderMarkdown should use custom Markdown transform function from props',
pageObjects.runHook('useRenderMarkdownAsHTML', [], fn => fn('Hello, World!'))
).resolves.toMatchInlineSnapshot(`"HELLO, WORLD!"`);
});

test('renderMarkdown should return falsy if the custom Markdown transform function is null', async () => {
const { pageObjects } = await setupWebDriver({
props: {
renderMarkdown: null
}
});

await expect(pageObjects.runHook('useRenderMarkdownAsHTML', [], fn => !!fn)).resolves.toMatchInlineSnapshot(`false`);
});
13 changes: 13 additions & 0 deletions __tests__/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ test('hero card', async () => {

expect(base64PNG).toMatchImageSnapshot(imageSnapshotOptions);
});

test('null renderMarkdown function', async () => {
const { driver, pageObjects } = await setupWebDriver({ props: { renderMarkdown: null } });

await pageObjects.sendMessageViaSendBox('echo **This text should be plain text**', { waitForSend: true });

await driver.wait(allImagesLoaded(), timeouts.fetch);
await driver.wait(minNumActivitiesShown(2), timeouts.directLine);

const base64PNG = await driver.takeScreenshot();

expect(base64PNG).toMatchImageSnapshot(imageSnapshotOptions);
});
3 changes: 2 additions & 1 deletion packages/bundle/src/useComposerProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function useComposerProps({ attachmentMiddleware, renderMarkdown,
return {
attachmentMiddleware: patchedAttachmentMiddleware,
extraStyleSet,
renderMarkdown: renderMarkdown || (text => defaultRenderMarkdown(text, patchedStyleOptions))
renderMarkdown:
typeof renderMarkdown === 'undefined' ? text => defaultRenderMarkdown(text, patchedStyleOptions) : renderMarkdown
};
}
7 changes: 5 additions & 2 deletions packages/component/src/hooks/useRenderMarkdownAsHTML.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useMemo } from 'react';

import useStyleOptions from '../hooks/useStyleOptions';
import useWebChatUIContext from './internal/useWebChatUIContext';
Expand All @@ -7,5 +7,8 @@ export default function useRenderMarkdownAsHTML() {
const { renderMarkdown } = useWebChatUIContext();
const [styleOptions] = useStyleOptions();

return useCallback(markdown => renderMarkdown(markdown, styleOptions), [renderMarkdown, styleOptions]);
return useMemo(() => renderMarkdown && (markdown => renderMarkdown(markdown, styleOptions)), [
renderMarkdown,
styleOptions
]);
}

0 comments on commit 7811093

Please sign in to comment.