Skip to content

Commit

Permalink
refactor: move angular loader in addon-storyshots framework part to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanmaisse committed Aug 4, 2019
1 parent e9e5c49 commit 6cbddb0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
13 changes: 12 additions & 1 deletion addons/storyshots/storyshots-core/src/frameworks/Loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
type SupportedFramework = 'angular';

export type RenderTree = (story: any) => any;

export interface Loader {
load: (options: any) => any;
load: (
options: any
) => {
framework: SupportedFramework;
renderTree: RenderTree;
renderShallowTree: any;
storybook: any;
};
test: (options: any) => boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'core-js';
import 'core-js/es/reflect';
import hasDependency from '../hasDependency';
import configure from '../configure';
import { Loader } from '../Loader';
import { StoryshotsOptions } from '../../api/StoryshotsOptions';

function setupAngularJestPreset() {
// Needed to prevent "Zone.js has detected that ZoneAwarePromise `(window|global).Promise` has been overwritten."
Expand All @@ -18,13 +20,13 @@ function setupAngularJestPreset() {
require.requireActual('jest-preset-angular/setupJest');
}

function test(options) {
function test(options: any): boolean {
return (
options.framework === 'angular' || (!options.framework && hasDependency('@storybook/angular'))
);
}

function load(options) {
function load(options: StoryshotsOptions) {
setupAngularJestPreset();

const { configPath, config } = options;
Expand All @@ -33,7 +35,7 @@ function load(options) {
configure({ configPath, config, storybook });

return {
framework: 'angular',
framework: 'angular' as const,
renderTree: require.requireActual('./renderTree').default,
renderShallowTree: () => {
throw new Error('Shallow renderer is not supported for angular');
Expand All @@ -42,7 +44,9 @@ function load(options) {
};
}

export default {
const angularLoader: Loader = {
load,
test,
};

export default angularLoader;
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import { initModuleData } from './helpers';
addSerializer(HTMLCommentSerializer);
addSerializer(AngularSnapshotSerializer);

function getRenderedTree(story) {
function getRenderedTree(story: any) {
const currentStory = story.render();

const { moduleMeta, AppComponent } = initModuleData(currentStory);

TestBed.configureTestingModule({
imports: [...moduleMeta.imports],
declarations: [...moduleMeta.declarations],
providers: [...moduleMeta.providers],
schemas: [NO_ERRORS_SCHEMA, ...moduleMeta.schemas],
bootstrap: [...moduleMeta.bootstrap],
});
TestBed.configureTestingModule(
// TODO: take a look at `bootstrap` because it looks it does not exists in TestModuleMetadata
{
imports: [...moduleMeta.imports],
declarations: [...moduleMeta.declarations],
providers: [...moduleMeta.providers],
schemas: [NO_ERRORS_SCHEMA, ...moduleMeta.schemas],
bootstrap: [...moduleMeta.bootstrap],
} as any
);

TestBed.overrideModule(BrowserDynamicTestingModule, {
set: {
Expand Down
1 change: 1 addition & 0 deletions addons/storyshots/storyshots-core/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module 'global';
declare module 'jest-preset-angular/*';

0 comments on commit 6cbddb0

Please sign in to comment.