0

在我的 Angular 应用程序中,有一个父子组件。父母将标题值提供给constructor状态的孩子。我正在尝试测试父组件的updateconstructor但是由于测试组件未添加的值而失败。

父组件.ts:

export class ShellUserViewComponent implements OnInit {

  @Output() OutwellcomeMsg: string | undefined;
  @Output() newTitle: string;
  @Output() users$: Observable<PropUser[]>;

  title = "Welcome to user!!";
  countString = "countLogic"; //static value
  count: number;

  constructor(private store: Store<AppState>) {
    this.count = 0;
    this.newTitle = this.countString; //updates the newTitle value
    this.users$ = of([]);
  }
}

父组件.spec.ts:(测试构造函数值):

describe('UserViewComponent', () => {

  beforeEach(async () => {
    await render(ShellUserViewComponent, {
      componentProperties: {
        title: "Welcome to chennai",
        countString: "I am new" //added new value as mock
      },
      declarations: [UserViewComponent],
      imports: [RouterTestingModule],
      providers: [provideMockStore({})]
    });
  })

  it('should find the welcome as title', async () => {
    expect(await screen.getByText(/Welcome to chennai/i)).toBeTruthy();
    expect(await screen.getByText(/I am new/i)).toBeTruthy(); //expecting in screen, fails
    screen.debug(); //no value finds. getting original value instead of mock value
  });

});

测试更新的正确方法是什么countString-?

4

0 回答 0