From 668e18032dc3776acafe2442b4410814ba9785a9 Mon Sep 17 00:00:00 2001 From: Isaries Date: Sun, 15 Nov 2026 17:50:56 +0800 Subject: [PATCH] fix(AdvancedProjectAuthoringComponent): Advanced settings throws for a unit that has never been saved A unit's metadata gets its authors when the unit is first saved. Opening Advanced Settings on a unit that has just been created and not yet saved makes ngOnInit() call some() on undefined authors, which throws "Cannot read properties of undefined (reading 'some')". A unit without authors is now treated as not the user's unit without throwing, so the Publish Unit link stays hidden until the first save, as it already was. --- ...vanced-project-authoring.component.spec.ts | 38 +++++++++++++++++++ .../advanced-project-authoring.component.ts | 5 +-- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.spec.ts diff --git a/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.spec.ts b/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.spec.ts new file mode 100644 index 00000000000..b02ae7cc5d1 --- /dev/null +++ b/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.spec.ts @@ -0,0 +1,38 @@ +import { AdvancedProjectAuthoringComponent } from './advanced-project-authoring.component'; + +const USER_ID = 7; + +function initialize(metadata: any): AdvancedProjectAuthoringComponent { + const configService = jasmine.createSpyObj('ConfigService', ['getContextPath', 'getProjectId']); + configService.getContextPath.and.returnValue(''); + configService.getProjectId.and.returnValue(1); + const projectService = jasmine.createSpyObj('TeacherProjectService', ['getProjectMetadata']); + projectService.getProjectMetadata.and.returnValue(metadata); + projectService.project = { metadata: metadata }; + const userService = jasmine.createSpyObj('UserService', ['getUserId']); + userService.getUserId.and.returnValue(USER_ID); + const component = new AdvancedProjectAuthoringComponent( + configService, + null, + projectService, + userService + ); + component.ngOnInit(); + return component; +} + +describe('AdvancedProjectAuthoringComponent', () => { + describe('ngOnInit()', () => { + it('should not treat a unit that has never been saved as the user unit', () => { + expect(initialize({ title: 'New Unit' })['isMyUnit']).toBeFalse(); + }); + + it('should treat a unit the user authored as the user unit', () => { + expect(initialize({ authors: [{ id: USER_ID }] })['isMyUnit']).toBeTrue(); + }); + + it('should not treat a unit someone else authored as the user unit', () => { + expect(initialize({ authors: [{ id: USER_ID + 1 }] })['isMyUnit']).toBeFalse(); + }); + }); +}); diff --git a/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.ts b/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.ts index 2df4c715d84..d33f0478294 100644 --- a/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.ts +++ b/src/assets/wise5/authoringTool/advanced/advanced-project-authoring.component.ts @@ -57,9 +57,8 @@ export class AdvancedProjectAuthoringComponent { this.navigationType = this.projectService.project.theme ?? 'default'; this.projectJSONString = JSON.stringify(this.projectService.project, null, 4); this.publishUnitUrl = `${this.configService.getContextPath()}/contact?projectId=${this.configService.getProjectId()}&publish=true`; - this.isMyUnit = this.metadata.authors.some( - (author) => author.id === this.userService.getUserId() - ); + this.isMyUnit = + this.metadata.authors?.some((author) => author.id === this.userService.getUserId()) ?? false; } protected tabChanged(event: MatTabChangeEvent): void {