Skip to content

Commit

Permalink
Angular: Fix issue if BrowserAnimationsModule is imported
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 24, 2023
1 parent bcca0cc commit c550dbb
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 394 deletions.
43 changes: 22 additions & 21 deletions code/frameworks/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@
},
"devDependencies": {
"@angular-devkit/architect": "^0.1500.4",
"@angular-devkit/build-angular": "^15.0.4",
"@angular-devkit/core": "^15.0.4",
"@angular/cli": "^15.0.4",
"@angular/common": "^15.0.4",
"@angular/compiler": "^15.0.4",
"@angular/compiler-cli": "^15.0.4",
"@angular/core": "^15.0.4",
"@angular/forms": "^15.0.4",
"@angular/platform-browser": "^15.0.4",
"@angular/platform-browser-dynamic": "^15.0.4",
"@angular-devkit/build-angular": "^15.1.1",
"@angular-devkit/core": "^15.1.1",
"@angular/animations": "^15.1.1",
"@angular/cli": "^15.1.1",
"@angular/common": "^15.1.1",
"@angular/compiler": "^15.1.1",
"@angular/compiler-cli": "^15.1.1",
"@angular/core": "^15.1.1",
"@angular/forms": "^15.1.1",
"@angular/platform-browser": "^15.1.1",
"@angular/platform-browser-dynamic": "^15.1.1",
"@types/rimraf": "^3.0.2",
"@types/tmp": "^0.2.3",
"cross-spawn": "^7.0.3",
Expand All @@ -90,17 +91,17 @@
"zone.js": "^0.12.0"
},
"peerDependencies": {
"@angular-devkit/architect": ">=0.1400.0",
"@angular-devkit/build-angular": ">=14.0.0",
"@angular-devkit/core": ">=14.0.0",
"@angular/cli": ">=14.0.0",
"@angular/common": ">=14.0.0",
"@angular/compiler": ">=14.0.0",
"@angular/compiler-cli": ">=14.0.0",
"@angular/core": ">=14.0.0",
"@angular/forms": ">=14.0.0",
"@angular/platform-browser": ">=14.0.0",
"@angular/platform-browser-dynamic": ">=14.0.0",
"@angular-devkit/architect": ">=0.1400.0 < 0.1600.0",
"@angular-devkit/build-angular": ">=14.1.0 < 16.0.0",
"@angular-devkit/core": ">=14.1.0 < 16.0.0",
"@angular/cli": ">=14.1.0 < 16.0.0",
"@angular/common": ">=14.1.0 < 16.0.0",
"@angular/compiler": ">=14.1.0 < 16.0.0",
"@angular/compiler-cli": ">=14.1.0 < 16.0.0",
"@angular/core": ">=14.1.0 < 16.0.0",
"@angular/forms": ">=14.1.0 < 16.0.0",
"@angular/platform-browser": ">=14.1.0 < 16.0.0",
"@angular/platform-browser-dynamic": ">=14.1.0 < 16.0.0",
"@babel/core": "*",
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
Expand Down
11 changes: 5 additions & 6 deletions code/frameworks/angular/src/builders/utils/run-compodoc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BuilderContext } from '@angular-devkit/architect';
import { LoggerApi } from '@angular-devkit/core/src/logger';
import { take } from 'rxjs/operators';

Expand Down Expand Up @@ -41,7 +40,7 @@ describe('runCompodoc', () => {
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
}
)
.pipe(take(1))
.subscribe();
Expand All @@ -65,7 +64,7 @@ describe('runCompodoc', () => {
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
}
)
.pipe(take(1))
.subscribe();
Expand All @@ -89,7 +88,7 @@ describe('runCompodoc', () => {
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
}
)
.pipe(take(1))
.subscribe();
Expand All @@ -112,7 +111,7 @@ describe('runCompodoc', () => {
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
}
)
.pipe(take(1))
.subscribe();
Expand All @@ -135,7 +134,7 @@ describe('runCompodoc', () => {
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
}
)
.pipe(take(1))
.subscribe();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NgModule, enableProdMode, Type, ApplicationRef } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideAnimations, BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { Subject, BehaviorSubject } from 'rxjs';
import { stringify } from 'telejson';
Expand Down Expand Up @@ -97,6 +98,16 @@ export abstract class AbstractRenderer {

const newStoryProps$ = new BehaviorSubject<ICollection>(storyFnAngular.props);

const hasAnimationsDefined =
!!storyFnAngular.moduleMetadata?.imports?.includes(BrowserAnimationsModule);

if (hasAnimationsDefined && storyFnAngular?.moduleMetadata?.imports) {
// eslint-disable-next-line no-param-reassign
storyFnAngular.moduleMetadata.imports = storyFnAngular.moduleMetadata.imports.filter(
(importedModule) => importedModule !== BrowserAnimationsModule
);
}

if (
!this.fullRendererRequired({
storyFnAngular,
Expand All @@ -122,7 +133,10 @@ export abstract class AbstractRenderer {
const application = getApplication({ storyFnAngular, component, targetSelector });

const applicationRef = await bootstrapApplication(application, {
providers: [storyPropsProvider(newStoryProps$)],
providers: [
storyPropsProvider(newStoryProps$),
...(hasAnimationsDefined ? [provideAnimations()] : []),
],
});

applicationRefs.add(applicationRef);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
:host {
display: block;
margin-top: 1rem;
}

.open-close-container {
border: 1px solid #dddddd;
margin-top: 1em;
padding: 20px 20px 0px 20px;
color: #000000;
font-weight: bold;
font-size: 20px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<nav>
<button type="button" (click)="toggle()">Toggle Open/Close</button>
</nav>

<div [@openClose]="isOpen ? 'open' : 'closed'" class="open-close-container">
<p>The box is now {{ isOpen ? 'Open' : 'Closed' }}!</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component } from '@angular/core';
// eslint-disable-next-line import/no-extraneous-dependencies
import { trigger, state, style, transition, animate } from '@angular/animations';

@Component({
selector: 'app-open-close',
animations: [
trigger('openClose', [
// ...
state(
'open',
style({
height: '200px',
opacity: 1,
backgroundColor: 'yellow',
})
),
state(
'closed',
style({
height: '100px',
opacity: 0.8,
backgroundColor: 'blue',
})
),
transition('open => closed', [animate('0.1s')]),
transition('closed => open', [animate('0.1s')]),
]),
],
templateUrl: 'open-close.component.html',
styleUrls: ['open-close.component.css'],
})
export class OpenCloseComponent {
isOpen = true;

toggle() {
this.isOpen = !this.isOpen;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Meta, StoryFn } from '@storybook/angular';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { within, userEvent } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
import { OpenCloseComponent } from './angular-src/open-close-component/open-close.component';

export default {
component: OpenCloseComponent,
parameters: {
chromatic: { delay: 100 },
},
} as Meta;

export const WithBrowserAnimations: StoryFn = () => ({
template: `<app-open-close></app-open-close>`,
moduleMetadata: {
declarations: [OpenCloseComponent],
imports: [BrowserAnimationsModule],
},
});

WithBrowserAnimations.play = async ({ canvasElement }) => {
const canvas = within(canvasElement);
const opened = canvas.getByText('The box is now Open!');
expect(opened).toBeDefined();
const submitButton = canvas.getByRole('button');
await userEvent.click(submitButton);
const closed = canvas.getByText('The box is now Closed!');
expect(closed).toBeDefined();
};
Loading

0 comments on commit c550dbb

Please sign in to comment.