我在测试后卫时遇到问题。 我是新手,但我不知道该怎么做。 我的后卫版本总是阻止带有窗口的页面离开,并且效果很好。 有人可以告诉我要测试此功能需要更改什么?
cab-deactivate.guard.ts
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { ComponentCanDeactivate } from './component-can-deactivate';
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<ComponentCanDeactivate> {
public canDeactivate(component: ComponentCanDeactivate): boolean {
if (!component.canDeactivate()){
if (confirm("You have unsaved changes! If you leave, your changes will be lost.")) {
return true;
}
return false;
}
return true;
}
}
can-deactivate.guard.spec.ts(不起作用)
import { MockComponent } from '@app/canvas/window/services/window.service.spec';
import { CanDeactivateGuard } from './can-deactivate.guard';
describe('[CanDeactivateGuard]', () => {
describe('canActivate', () => {
let canDeactivateGuard: CanDeactivateGuard;
let testValue;
let mockComponent;
it('should return false', () => {
canDeactivateGuard = new CanDeactivateGuard();
mockComponent = new MockComponent()
testValue = canDeactivateGuard.canDeactivate(mockComponent);
expect(testValue).toEqual(false);
});
it('should return true', () => {
canDeactivateGuard = new CanDeactivateGuard();
mockComponent = new MockComponent()
testValue = canDeactivateGuard.canDeactivate(mockComponent);
expect(testValue).toEqual(true);
});
});
});