diff --git a/code/jest.config.js b/code/jest.config.js index a2ffa022105e..a050eb131d11 100644 --- a/code/jest.config.js +++ b/code/jest.config.js @@ -40,6 +40,7 @@ module.exports = { ], roots: [ '/addons', + '/renderers', '/frameworks', '/lib', '/examples/official-storybook', diff --git a/code/renderers/react/src/render.test.tsx b/code/renderers/react/src/render.test.tsx new file mode 100644 index 000000000000..e03813227f67 --- /dev/null +++ b/code/renderers/react/src/render.test.tsx @@ -0,0 +1,58 @@ +import React, { useEffect } from 'react'; +import { jest, describe, it, expect } from '@jest/globals'; + +import { renderToDOM } from './render'; + +jest.setTimeout(500); + +describe('react renderer', () => { + it('waits until the component has rendered', async () => { + const domElement = document.createElement('div'); + const unboundStoryFn = jest.fn(() => 'Story'); + const showMain = jest.fn(); + + await renderToDOM({ unboundStoryFn, showMain } as any, domElement); + expect(unboundStoryFn).toHaveBeenCalledTimes(1); + expect(domElement.innerHTML).toBe('Story'); + }); + + it('re-renders the story when called again', async () => { + const domElement = document.createElement('div'); + const unboundStoryFn = jest.fn(() => 'Story'); + const showMain = jest.fn(); + + await renderToDOM({ unboundStoryFn, showMain } as any, domElement); + await renderToDOM({ unboundStoryFn, showMain } as any, domElement); + expect(unboundStoryFn).toHaveBeenCalledTimes(2); + }); + + it('does not remount the component when called a second time', async () => { + const domElement = document.createElement('div'); + const effect = jest.fn(); + const Component = () => { + useEffect(effect as any, []); + return
Component
; + }; + const unboundStoryFn = jest.fn(() => ); + const showMain = jest.fn(); + + await renderToDOM({ unboundStoryFn, showMain } as any, domElement); + await renderToDOM({ unboundStoryFn, showMain } as any, domElement); + expect(effect).toHaveBeenCalledTimes(1); + }); + + it('does remount the component when called with forceRemount', async () => { + const domElement = document.createElement('div'); + const effect = jest.fn(); + const Component = () => { + useEffect(effect as any, []); + return
Component
; + }; + const unboundStoryFn = jest.fn(() => ); + const showMain = jest.fn(); + + await renderToDOM({ unboundStoryFn, showMain } as any, domElement); + await renderToDOM({ unboundStoryFn, showMain, forceRemount: true } as any, domElement); + expect(effect).toHaveBeenCalledTimes(2); + }); +});