Skip to content
Snippets Groups Projects
reference-manager.spec.ts 6.42 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { TestBed } from '@angular/core/testing';
    import * as singleElement from 'test-data/unit-definitions/reference-testing/single-element.json';
    import * as elementRef from 'test-data/unit-definitions/reference-testing/element-ref.json';
    import * as elementRef2 from 'test-data/unit-definitions/reference-testing/2elements-ref.json';
    
    rhenck's avatar
    rhenck committed
    import * as section1 from 'test-data/unit-definitions/reference-testing/section-deletion.json';
    import * as section2 from 'test-data/unit-definitions/reference-testing/section2.json';
    import * as pageRefs from 'test-data/unit-definitions/reference-testing/pageRefs.json';
    import * as cloze from 'test-data/unit-definitions/reference-testing/cloze.json';
    import * as pageNav from 'test-data/unit-definitions/reference-testing/pageNav.json';
    
    import { DropListElement } from 'common/models/elements/input-elements/drop-list';
    import { APIService } from 'common/shared.module';
    import { MatSnackBarModule } from '@angular/material/snack-bar';
    import { MatDialogModule } from '@angular/material/dialog';
    import { TranslateModule } from '@ngx-translate/core';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    import { AudioElement } from 'common/models/elements/media-elements/audio';
    import { Section } from 'common/models/section';
    
    rhenck's avatar
    rhenck committed
    import { Page, PageProperties } from 'common/models/page';
    import { ClozeElement, ClozeProperties } from 'common/models/elements/compound-elements/cloze/cloze';
    import { ReferenceManager } from 'editor/src/app/services/reference-manager';
    import { Unit, UnitProperties } from 'common/models/unit';
    
    
    describe('ReferenceManager', () => {
      class ApiStubService {
        // eslint-disable-next-line class-methods-use-this
        getResourceURL(): string {
          return 'assets';
        }
      }
    
      beforeEach(() => {
        TestBed.configureTestingModule({
          providers: [
            { provide: APIService, useClass: ApiStubService }
          ],
          imports: [MatSnackBarModule, MatDialogModule, BrowserAnimationsModule, TranslateModule.forRoot()]
        });
      });
    
      it('should load data', () => {
        expect(singleElement).toBeTruthy();
      });
    
      it('should find no refs for single element', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(singleElement as unknown as UnitProperties));
    
        const element = {
          type: 'drop-list',
          id: 'drop-list_1',
          label: 'Beschriftung',
          value: [],
          connectedTo: []
        } as unknown as DropListElement;
    
    rhenck's avatar
    rhenck committed
        expect(refMan.getElementsReferences([element]))
    
          .toEqual([]);
      });
    
      it('should find refs when deleting element', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(elementRef as unknown as UnitProperties));
    
        const element = {
          type: 'drop-list',
          id: 'drop-list_1',
          label: 'Beschriftung',
          value: [],
          connectedTo: []
        } as unknown as DropListElement;
    
    
    rhenck's avatar
    rhenck committed
        expect(refMan.getElementsReferences([element])
          .length).toEqual(1);
        expect(refMan.getElementsReferences([element])[0].element)
    
          .toEqual(jasmine.objectContaining({ ...element }));
    
    rhenck's avatar
    rhenck committed
        expect(refMan.getElementsReferences([element])[0].refs[0])
    
          .toEqual(jasmine.objectContaining({
            type: 'drop-list',
            id: 'drop-list_2'
          }));
      });
    
      it('should find 2 refs when deleting 2 elements', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(elementRef2 as unknown as UnitProperties));
    
        const element1 = {
          type: 'drop-list',
          id: 'drop-list_1',
          value: [],
          connectedTo: []
        } as unknown as DropListElement;
        const element2 = {
          type: 'audio',
          id: 'audio_1'
        } as AudioElement;
    
    
    rhenck's avatar
    rhenck committed
        const refs = refMan.getElementsReferences([element1, element2]);
    
    
        expect(refs.length)
          .toEqual(2);
        expect(refs[0].element)
          .toEqual(jasmine.objectContaining({ ...element1 }));
        expect(refs[1].element)
          .toEqual(jasmine.objectContaining({
            type: 'audio',
            id: 'audio_1'
          }));
        expect(refs[1].refs[0])
          .toEqual(jasmine.objectContaining({
            type: 'audio',
            id: 'audio_2'
          }));
      });
    
      it('should find ref when deleting section', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(section1 as unknown as UnitProperties));
        const section = JSON.parse(JSON.stringify(section1)).pages[0].sections[0] as unknown as Section;
        const refs = refMan.getSectionElementsReferences([section]);
    
    
        expect(refs.length)
          .toEqual(1);
        expect(refs[0].refs[0])
          .toEqual(jasmine.objectContaining({
            type: 'drop-list',
            id: 'drop-list_3'
          }));
      });
    
      it('should find refs when deleting section but ignore refs within same section', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(section2 as unknown as UnitProperties));
        const section = JSON.parse(JSON.stringify(section2)).pages[0].sections[0] as unknown as Section;
        const refs = refMan.getSectionElementsReferences([section]);
    
    
        expect(refs.length)
          .toEqual(1);
        expect(refs[0].refs[0])
          .toEqual(jasmine.objectContaining({
            type: 'drop-list',
            id: 'drop-list_3'
          }));
      });
    
      it('should ignore refs within same page', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(pageRefs as unknown as UnitProperties));
        const page = new Page(JSON.parse(JSON.stringify(pageRefs)).pages[0] as unknown as PageProperties);
        const refs = refMan.getPageElementsReferences(page);
    
    
        expect(refs.length)
          .toEqual(1);
        expect(refs[0].refs[0])
          .toEqual(jasmine.objectContaining({
            type: 'drop-list',
            id: 'drop-list_3'
          }));
      });
    
      it('should find cloze refs but ignore refs within same cloze', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(cloze as unknown as UnitProperties));
        const clozeElement = new ClozeElement(
          JSON.parse(JSON.stringify(cloze)).pages[0].sections[0].elements[0] as unknown as ClozeProperties);
        const refs = refMan.getElementsReferences([clozeElement]);
    
    
        expect(refs.length)
          .toEqual(1);
        expect(refs[0].refs[0])
          .toEqual(jasmine.objectContaining({
            type: 'drop-list',
            id: 'drop-list_3'
          }));
      });
    
      it('should find page refs via buttons', () => {
    
    rhenck's avatar
    rhenck committed
        const refMan = new ReferenceManager(new Unit(pageNav as unknown as UnitProperties));
        const refs = refMan.getButtonReferencesForPage(0);
    
    rhenck's avatar
    rhenck committed
        expect(refs[0].refs.length)
          .toEqual(1);
        expect(refs[0].refs[0])
    
          .toEqual(jasmine.objectContaining({
            type: 'button',
            id: 'button_2'
          }));
      });
    });