I am working with the following interface
export interface Command {
id: CommandId;
disabled: boolean;
}
My goal is to verify that the 'disabled' property has been changed. Here are my attempts:
1) Creating an object and checking if the property has been updated:
const command1: Command = {id: CommandId.Print, disabled: false};
// additional code here (refer below)
expect(command1.disabled).toBe(true);
Result:
TypeError: Cannot set property 'disabled' of undefined
2) Trying to create a Mock using typemock:
const command1 = Mock.ofType<Command>();
command1.setup(x => x.id).returns(() => CommandId.Print);
// additional code here (refer below)
command1.verify(x => x.disabled, Times.once());
Result:
TypeError: 'set' on proxy: trap returned falsish for property 'disabled'
3) Attempting to use spyOnProperty:
const command1 = {id: CommandId.Print, disabled: false} as Command;
spyOnProperty(command1, 'disabled');
// additional code following (refer below)
Result:
Error: Property disabled does not have access type get
I am struggling to find a solution to verify this. (I am still learning Angular and typescript)
The entire test method looks like this:
// arrange
const command1 = {id: TaskCommandId.Print, disabled: false} as Command;
spyOnProperty(command1, 'disabled');
const command2 = {id: CommandId.SaveTemplate, disabled: false } as Command;
spyOnProperty(command2, 'disabled');
const commands = [command1, command2];
mockService.setup(x => x.getCommands()).returns(() => commands);
const command1Update = {id: CommandId.Print, disabled: true } as CommandState;
component.ngOnInit();
// act
component.updateEnabledState(command1Update);
// assert
expect(command1.disabled).toHaveBeenCalled();
expect(command2.disabled).not.toHaveBeenCalled();