Fix some of the Angular tests

This commit is contained in:
Mark 2022-10-24 23:23:14 -05:00
parent 9255a00914
commit 22bcdd0d89
5 changed files with 22 additions and 8 deletions

View File

@ -4,7 +4,7 @@ import { schema } from '@angular-devkit/core';
import * as path from 'path';
const buildStandaloneMock = jest.fn();
jest.doMock('@storybook/angular/standalone', () => buildStandaloneMock);
jest.doMock('@storybook/core-server/standalone', () => buildStandaloneMock);
jest.doMock('find-up', () => ({ sync: () => './storybook/tsconfig.ts' }));
const cpSpawnMock = {

View File

@ -4,7 +4,7 @@ import { schema } from '@angular-devkit/core';
import * as path from 'path';
const buildStandaloneMock = jest.fn();
jest.doMock('@storybook/angular/standalone', () => buildStandaloneMock);
jest.doMock('@storybook/core-server/standalone', () => buildStandaloneMock);
jest.doMock('find-up', () => ({ sync: () => './storybook/tsconfig.ts' }));
const cpSpawnMock = {

View File

@ -51,6 +51,7 @@ describe('runCompodoc', () => {
['compodoc', '-p', 'path/to/tsconfig.json', '-d', 'path/to/project'],
{
cwd: 'path/to/project',
shell: true,
}
);
});
@ -74,6 +75,7 @@ describe('runCompodoc', () => {
['compodoc', '-d', 'path/to/project', '-p', 'path/to/tsconfig.stories.json'],
{
cwd: 'path/to/project',
shell: true,
}
);
});

View File

@ -18,8 +18,8 @@ describe('RendererFactory', () => {
beforeEach(async () => {
rendererFactory = new RendererFactory();
document.body.innerHTML =
'<div id="root"></div><div id="root-docs"><div id="story-in-docs"></div></div>';
rootTargetDOMNode = global.document.getElementById('root');
'<div id="storybook-root"></div><div id="root-docs"><div id="story-in-docs"></div></div>';
rootTargetDOMNode = global.document.getElementById('storybook-root');
rootDocstargetDOMNode = global.document.getElementById('root-docs');
(platformBrowserDynamic as any).mockImplementation(platformBrowserDynamicTesting);
jest.spyOn(console, 'log').mockImplementation(() => {});
@ -301,14 +301,16 @@ describe('RendererFactory', () => {
});
it('should reset root HTML', async () => {
global.document.getElementById('root').appendChild(global.document.createElement('👾'));
global.document
.getElementById('storybook-root')
.appendChild(global.document.createElement('👾'));
expect(global.document.getElementById('root').innerHTML).toContain('Canvas 🖼');
expect(global.document.getElementById('storybook-root').innerHTML).toContain('Canvas 🖼');
const render = await rendererFactory.getRendererInstance(
'my-story-in-docs',
rootDocstargetDOMNode
);
expect(global.document.getElementById('root').innerHTML).toBe('');
expect(global.document.getElementById('storybook-root').innerHTML).toBe('');
});
});

View File

@ -7,6 +7,7 @@ import { storyPropsProvider } from './StorybookProvider';
import { isComponentAlreadyDeclaredInModules } from './utils/NgModulesAnalyzer';
import { isDeclarable, isStandaloneComponent } from './utils/NgComponentAnalyzer';
import { createStorybookWrapperComponent } from './StorybookWrapperComponent';
import { computesTemplateFromComponent } from './ComputesTemplateFromComponent';
export const getStorybookModuleMetadata = (
{
@ -21,7 +22,12 @@ export const getStorybookModuleMetadata = (
storyProps$: Subject<ICollection>
): NgModule => {
const { props, styles, moduleMetadata = {} } = storyFnAngular;
const { template } = storyFnAngular;
let { template } = storyFnAngular;
const hasTemplate = !hasNoTemplate(template);
if (!hasTemplate && component) {
template = computesTemplateFromComponent(component, props, '');
}
/**
* Create a component that wraps generated template and gives it props
@ -68,3 +74,7 @@ export const createStorybookModule = (ngModule: NgModule): Type<unknown> => {
class StorybookModule {}
return StorybookModule;
};
function hasNoTemplate(template: string | null | undefined): template is undefined {
return template === null || template === undefined;
}