简体   繁体   English

Angular 4 + Jest-Preset-Angular:使用从文件导入的触发器在主机上测试动画

[英]Angular 4 + Jest-Preset-Angular : testing animation on host with trigger imported from file

I am having an issue issue when running tests with Jest-Preset-Angular on a component that has an animation on host element imported from a seperate file (so it can be reused in another component). 当在一个组件上运行Jest-Preset-Angular的测试时,我遇到一个问题,该组件在从单独文件导入的宿主元素上具有动画(因此可以在另一个组件中重用)。

example.animation.ts example.animation.ts

import { trigger } from '@angular/animations';

export const ExampleAnimationHost = { '[@example]': '' };
export const ExampleAnimationTrigger = trigger('example', []);

example.component.ts example.component.ts

import { Component } from '@angular/core';
import { ExampleAnimationHost, ExampleAnimationTrigger } from './example.animation';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss'],
  animations: [ExampleAnimationTrigger],
  host: ExampleAnimationHost,
})
export class ExampleComponent { }

The fact is that if I copy-paste the trigger into the animations property of the @Component decorator my test pass ... otherwise it fails as it doesn't seems to find the animation declaration. 事实是,如果我触发器复制粘贴@Component装饰器的animations属性中,那么我的测试通过...否则它将失败,因为它似乎找不到动画声明。

Any ideas? 有任何想法吗?

I will answer my own question if any body ever have the same problem. 如果有人有同样的问题,我将回答我自己的问题。

There is an issue here specifying that you need to put the animations property before the styleUrls property in the @Component decorator. 此处存在一个问题指明您需要在@Component装饰器中将animations属性放到styleUrls属性之前

import { Component } from '@angular/core';
import { ExampleAnimationHost, ExampleAnimationTrigger } from './example.animation';

@Component({
  animations: [ExampleAnimationTrigger], // must be place before styleUrls
  host: ExampleAnimationHost,
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss'],
})
export class ExampleComponent { }

And voila! 瞧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM