断言模板

断言模板(assertion template)提供一个可复用的基本模板来断言部件的整个输出内容,但在执行每个测试前可按需修改部分内容。这意味着在多次测试中都不会改变的公共元素可被抽象并定义一次,然后多处使用。

要使用断言模板,首先导入模块:

  1. import assertionTemplate from '@dojo/framework/testing/assertionTemplate';

可创建一个基本断言,它定义了部件的默认渲染状态。假定有以下部件:

src/widgets/Profile.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import * as css from './styles/Profile.m.css';
  3. export interface ProfileProperties {
  4. username?: string;
  5. }
  6. const factory = create().properties<ProfileProperties>();
  7. const Profile = factory(function Profile({ properties }) {
  8. const { username } = properties();
  9. return <h1 classes={[css.root]}>{`Welcome ${username || 'Stranger'}!`}</h1>;
  10. });
  11. export default Profile;

基本断言如下所示:

tests/unit/widgets/Profile.tsx

  1. const { describe, it } = intern.getInterface('bdd');
  2. import harness from '@dojo/framework/testing/harness';
  3. import assertionTemplate from '@dojo/framework/testing/assertionTemplate';
  4. import { tsx } from '@dojo/framework/core/vdom';
  5. import Profile from '../../../src/widgets/Profile';
  6. import * as css from '../../../src/widgets/Profile.m.css';
  7. const profileAssertion = assertionTemplate(() => (
  8. <h1 classes={[css.root]} assertion-key="welcome">
  9. Welcome Stranger!
  10. </h1>
  11. ));

在测试中这样写:

tests/unit/widgets/Profile.tsx

  1. const profileAssertion = assertionTemplate(() => (
  2. <h1 classes={[css.root]} assertion-key="welcome">
  3. Welcome Stranger!
  4. </h1>
  5. ));
  6. describe('Profile', () => {
  7. it('default renders correctly', () => {
  8. const h = harness(() => <Profile />);
  9. h.expect(profileAssertion);
  10. });
  11. });

要测试为 Profile 传入 username 属性的场景,可以按如下方式为断言模板调参:

tests/unit/widgets/Profile.tsx

  1. describe('Profile', () => {
  2. ...
  3. it('renders given username correctly', () => {
  4. // 使用给定的用户名更新期望的结果
  5. const namedAssertion = profileAssertion.setChildren('~welcome', () => [
  6. 'Welcome Kel Varnsen!'
  7. ]);
  8. const h = harness(() => <Profile username="Kel Varnsen" />);
  9. h.expect(namedAssertion);
  10. });
  11. });

这里使用 baseAssertion 的 setChildren() api,然后使用特定的 ~ 选择器来定位 key 值为 ~welcome 的节点。assertion-key 属性(当使用 w()v() 函数时为 ~key)是断言模板的一个特殊属性,在断言时会被删除,因此在匹配渲染结构时不会显示出来。此功能能让断言模板简单的选择节点,而不需要扩展实际的部件渲染函数。一旦找到 welcome 节点,它的子节点将被设置为新值 ['Welcome Kel Varnsen!'],然后在 h.expect 中使用生成的模板。需要注意的是,断言模板在设置值时总是返回一个新的断言模板,这可以确保现有模板不会被意外地修改,若被修改可能导致其他测试失败,并允许基于新模板,增量逐层构建出新的模板。

断言模板具有以下 API:

  1. insertBefore(selector: string, children: () => DNode[]): AssertionTemplateResult;
  2. insertAfter(selector: string, children: () => DNode[]): AssertionTemplateResult;
  3. insertSiblings(selector: string, children: () => DNode[], type?: 'before' | 'after'): AssertionTemplateResult;
  4. append(selector: string, children: () => DNode[]): AssertionTemplateResult;
  5. prepend(selector: string, children: () => DNode[]): AssertionTemplateResult;
  6. replaceChildren(selector: string, children: () => DNode[]): AssertionTemplateResult;
  7. setChildren(selector: string, children: () => DNode[], type?: 'prepend' | 'replace' | 'append'): AssertionTemplateResult;
  8. setProperty(selector: string, property: string, value: any): AssertionTemplateResult;
  9. setProperties(selector: string, value: any | PropertiesComparatorFunction): AssertionTemplateResult;
  10. getChildren(selector: string): DNode[];
  11. getProperty(selector: string, property: string): any;
  12. getProperties(selector: string): any;
  13. replace(selector: string, node: DNode): AssertionTemplateResult;
  14. remove(selector: string): AssertionTemplateResult;