From 5d9df5456283311ccfdfa05a812306312f7d6c48 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Wed, 7 Aug 2024 12:58:17 +0200 Subject: [PATCH] feat: unit and e2e testing --- apps/metadata-editor-e2e/src/e2e/edit.cy.ts | 45 +++++++++++++++++++ .../record-form/record-form.component.spec.ts | 15 +++++++ .../check-toggle.component.spec.ts | 19 ++++++++ 3 files changed, 79 insertions(+) diff --git a/apps/metadata-editor-e2e/src/e2e/edit.cy.ts b/apps/metadata-editor-e2e/src/e2e/edit.cy.ts index ed249ca863..4f14101cca 100644 --- a/apps/metadata-editor-e2e/src/e2e/edit.cy.ts +++ b/apps/metadata-editor-e2e/src/e2e/edit.cy.ts @@ -57,4 +57,49 @@ describe('editor form', () => { }) cy.get('md-editor-publish-button').click() }) + + describe('Access and contact', () => { + describe('Open data switch', () => { + describe('When the open data switch is checked', () => { + beforeEach(() => { + cy.visit('/edit/accroche_velos') + cy.get('md-editor-page-selector').find('gn-ui-button').last().click() + }) + it('should not display the licence section', () => { + cy.get('gn-ui-form-field-license').should('not.exist') + }) + it('should display the license section when toggled', () => { + cy.get('gn-ui-check-toggle').find('span').first().click() + cy.get('gn-ui-form-field-license').should('be.visible') + cy.get('gn-ui-form-field-license') + .find('button') + .children('div') + .first() + .invoke('text') + .should('eq', ' Open Licence (Etalab) ') + }) + }) + describe('When the open data switch is unchecked', () => { + beforeEach(() => { + cy.visit( + '/edit/fr-120066022-jdd-f20f8125-877e-46dc-8cf8-2a8a372045eb' + ) + cy.get('md-editor-page-selector').find('gn-ui-button').last().click() + }) + it('should display the licence section', () => { + cy.get('gn-ui-form-field-license').should('be.visible') + cy.get('gn-ui-form-field-license') + .find('button') + .children('div') + .first() + .invoke('text') + .should('eq', ' Creative Commons CC-BY ') + }) + it('should hide the license section when toggled', () => { + cy.get('gn-ui-check-toggle').find('span').first().click() + cy.get('gn-ui-form-field-license').should('not.exist') + }) + }) + }) + }) }) diff --git a/libs/feature/editor/src/lib/components/record-form/record-form.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/record-form.component.spec.ts index 2042e4c857..63881e2bc5 100644 --- a/libs/feature/editor/src/lib/components/record-form/record-form.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/record-form.component.spec.ts @@ -39,4 +39,19 @@ describe('RecordFormComponent', () => { ) }) }) + + describe('onOpenDataToggled', () => { + it('should set isHidden and call the facade to set the license', () => { + component.onOpenDataToggled([true, [{ text: 'license' }]]) + expect(component.isHidden).toBe(true) + expect(component.facade.updateRecordField).toHaveBeenCalledWith( + 'licenses', + [ + { + text: 'license', + }, + ] + ) + }) + }) }) diff --git a/libs/ui/inputs/src/lib/check-toggle/check-toggle.component.spec.ts b/libs/ui/inputs/src/lib/check-toggle/check-toggle.component.spec.ts index dbad56689d..351e9d1687 100644 --- a/libs/ui/inputs/src/lib/check-toggle/check-toggle.component.spec.ts +++ b/libs/ui/inputs/src/lib/check-toggle/check-toggle.component.spec.ts @@ -1,12 +1,17 @@ import { ComponentFixture, TestBed } from '@angular/core/testing' import { CheckToggleComponent } from './check-toggle.component' +import { getGlobalConfig } from '@geonetwork-ui/util/app-config' + +jest.mock('@geonetwork-ui/util/app-config') describe('CheckToggleComponent', () => { let component: CheckToggleComponent let fixture: ComponentFixture beforeEach(async () => { + ;(getGlobalConfig as jest.Mock).mockReturnValue({ LICENSES: ['CC-BY'] }) + await TestBed.configureTestingModule({ imports: [CheckToggleComponent], }).compileComponents() @@ -19,4 +24,18 @@ describe('CheckToggleComponent', () => { it('should create', () => { expect(component).toBeTruthy() }) + + it('should emit the new license value on toggle', () => { + const toggledSpy = jest.spyOn(component.toggled, 'emit') + component.model = 'licenses' + component.toggle(true) + expect(toggledSpy).toHaveBeenCalledWith([true, [{ text: 'CC-BY' }]]) + }) + + it('should emit the event value on toggle when model is not licenses', () => { + const toggledSpy = jest.spyOn(component.toggled, 'emit') + component.model = 'not-licenses' + component.toggle(true) + expect(toggledSpy).toHaveBeenCalledWith(true) + }) })