Skip to content

Commit

Permalink
Merge branch 'master' into build-storybook-watch-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-dv authored Jan 31, 2018
2 parents b01e3cb + 9a597d4 commit 42a8126
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 8 deletions.
7 changes: 4 additions & 3 deletions addons/knobs/src/angular/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ const getAnnotatedComponent = ({ componentMeta, component, params, knobStore, ch
return KnobWrapperComponent;
};

const createComponentFromTemplate = template => {
const createComponentFromTemplate = (template, styles) => {
const componentClass = class DynamicComponent {};

return Component({
template,
styles,
})(componentClass);
};

Expand All @@ -106,10 +107,10 @@ export function prepareComponent({ getStory, context, channel, knobStore }) {
resetKnobs(knobStore, channel);
const story = getStory(context);
let { component } = story;
const { template } = story;
const { template, styles } = story;

if (!component) {
component = createComponentFromTemplate(template);
component = createComponentFromTemplate(template, styles);
}

const { componentMeta, props, params, moduleMetadata } = getComponentMetadata({
Expand Down
24 changes: 24 additions & 0 deletions addons/storyshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ Now run your Jest test command. (Usually, `npm test`.) Then you can see all of y
![Screenshot](docs/storyshots.png)


### Using `createNodeMock` to mock refs

`react-test-renderer` doesn't provide refs for rendered components. By
default, it returns null when the refs are referenced. In order to mock
out elements that rely on refs, you will have to use the
`createNodeMock` option [added to React](https://reactjs.org/blog/2016/11/16/react-v15.4.0.html#mocking-refs-for-snapshot-testing) starting with version 15.4.0.

Here is an example of how to specify the `createNodeMock` option in Storyshots:

```js
import initStoryshots, { snapshotWithOptions } from '@storybook/addon-storyshots'
import TextareaThatUsesRefs from '../component/TextareaThatUsesRefs'

initStoryshots({
test: snapshotWithOptions({
createNodeMock: (element) => {
if (element.type === TextareaThatUsesRefs) {
return document.createElement('textarea')
}
},
}),
})
```

## Configure Storyshots for image snapshots ( alpha )

/*\ **React-native** is **not supported** by this test function.
Expand Down
11 changes: 6 additions & 5 deletions app/angular/src/client/preview/angular/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,27 @@ const getModule = (
return NgModule(moduleMeta)(moduleClass);
};

const createComponentFromTemplate = (template: string): Function => {
const createComponentFromTemplate = (template: string, styles: string[]): Function => {
const componentClass = class DynamicComponent {};

return Component({
template: template,
template,
styles,
})(componentClass);
};

const initModule = (
currentStory: IGetStoryWithContext,
context: IContext,
reRender: boolean = false,
reRender: boolean = false
): Function => {
const storyObj = currentStory(context);
const { component, template, props, moduleMetadata = {} } = storyObj;
const { component, template, props, styles, moduleMetadata = {} } = storyObj;

let AnnotatedComponent;

if (template) {
AnnotatedComponent = createComponentFromTemplate(template);
AnnotatedComponent = createComponentFromTemplate(template, styles);
} else {
AnnotatedComponent = component;
}
Expand Down
1 change: 1 addition & 0 deletions app/angular/src/client/preview/angular/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface NgStory {
propsMeta?: ICollection;
moduleMetadata?: NgModuleMetadata;
template?: string;
styles?: string[];
}

export interface NgError {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Custom Style Default 1`] = `
<storybook-dynamic-app-root
cfr={[Function CodegenComponentFactoryResolver]}
data={[Function Object]}
target={[Function ViewContainerRef_]}
>
<ng-component>
<storybook-button-component
_nghost-c9=""
ng-reflect-text="Button with custom styles"
>


<button
_ngcontent-c9=""
>
Button with custom styles
</button>


</storybook-button-component>
</ng-component>
</storybook-dynamic-app-root>
`;

exports[`Storyshots Custom Style With Knobs 1`] = `
<storybook-dynamic-app-root
cfr={[Function CodegenComponentFactoryResolver]}
data={[Function Object]}
target={[Function ViewContainerRef_]}
>
<ng-component
_nghost-c10=""
>
<storybook-button-component
_ngcontent-c10=""
_nghost-c11=""
ng-reflect-text="Button with custom styles"
>


<button
_ngcontent-c11=""
>
Button with custom styles
</button>


</storybook-button-component>
</ng-component>
</storybook-dynamic-app-root>
`;
43 changes: 43 additions & 0 deletions examples/angular-cli/src/stories/custom-styles.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { storiesOf } from '@storybook/angular';
import { action } from '@storybook/addon-actions';
import { withKnobs, text } from '@storybook/addon-knobs/angular';
import { Button } from '@storybook/angular/demo';

storiesOf('Custom Style', module)
.add('Default', () => ({
template: `<storybook-button-component [text]="text" (onClick)="onClick($event)"></storybook-button-component>`,
moduleMetadata: {
declarations: [Button],
},
props: {
text: 'Button with custom styles',
onClick: action('log'),
},
styles: [
`
storybook-button-component {
background-color: yellow;
padding: 25px;
}
`,
],
}))
.addDecorator(withKnobs)
.add('With Knobs', () => ({
template: `<storybook-button-component [text]="text" (onClick)="onClick($event)"></storybook-button-component>`,
moduleMetadata: {
declarations: [Button],
},
props: {
text: text('text', 'Button with custom styles'),
onClick: action('log'),
},
styles: [
`
storybook-button-component {
background-color: red;
padding: 25px;
}
`,
],
}));

0 comments on commit 42a8126

Please sign in to comment.