Skip to content

Commit

Permalink
Up pages tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Oct 8, 2023
1 parent ed58ba7 commit 4f3af74
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export default class PageManager extends ItemManagerModule<PageManagerConfig, Pa
return page;
};
!opts.silent && em.trigger(evPageAddBefore, props, add, opts);
return !opts.abort && add();
return !opts.abort ? add() : undefined;
}

/**
Expand Down
79 changes: 37 additions & 42 deletions test/specs/pages/index.js → test/specs/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import Editor from 'editor';
import { ComponentDefinition } from '../../../src/dom_components/model/types';
import Editor from '../../../src/editor';
import EditorModel from '../../../src/editor/model/Editor';

describe('Pages', () => {
let editor;
let em;
let domc;
let initCmpLen;
let pm;
let editor: Editor;
let em: EditorModel;
let domc: Editor['Components'];
let initCmpLen = 0;
let pm: Editor['Pages'];

beforeAll(() => {
editor = new Editor({ pageManager: true });
editor = new Editor({ pageManager: {} });
em = editor.getModel();
domc = em.get('DomComponents');
pm = em.get('PageManager');
domc = em.Components;
pm = em.Pages;
pm.onLoad();
initCmpLen = Object.keys(domc.allById()).length;
});

afterAll(() => {
editor.destroy();
pm = 0;
em = 0;
domc = 0;
});

beforeEach(() => {});
afterEach(() => {});

test('Pages module exists', () => {
expect(pm).toBeTruthy();
});
Expand Down Expand Up @@ -58,7 +54,7 @@ describe('Pages', () => {

test('Adding new page with selection', () => {
const name = 'Test page';
const page = pm.add({ name }, { select: 1 });
const page = pm.add({ name }, { select: true })!;
expect(page.id).toBeTruthy();
expect(page.get('name')).toBe(name);
expect(pm.getSelected()).toBe(page);
Expand All @@ -69,19 +65,24 @@ describe('Pages', () => {
});

describe('Init with pages', () => {
let idPage1, idComp1, idComp2, comp1, comp2, initPages, allbyId;
const createCompDef = id => ({
let idPage1 = 'page-1';
let idComp1 = 'comp1';
let idComp2 = 'comp2';
let comp1: ComponentDefinition;
let comp2: ComponentDefinition;
let initPages;
let allbyId: ReturnType<Editor['Components']['allById']>;

const createCompDef = (id: string): ComponentDefinition => ({
attributes: {
id,
class: id,
customattr: id,
},
components: `Component ${id}`,
});

beforeAll(() => {
idPage1 = 'page-1';
idComp1 = 'comp1';
idComp2 = 'comp2';
comp1 = createCompDef(idComp1);
comp2 = createCompDef(idComp2);
initPages = [
Expand Down Expand Up @@ -124,9 +125,6 @@ describe('Pages', () => {

afterAll(() => {
editor.destroy();
pm = 0;
em = 0;
domc = 0;
});

test('Pages are created correctly', () => {
Expand All @@ -153,26 +151,23 @@ describe('Pages', () => {
});

describe('Managing pages', () => {
let editor;
let em;
let domc;
let initCmpLen;
let pm;
let editor: Editor;
let em: EditorModel;
let domc: Editor['Components'];
let initCmpLen = 0;
let pm: Editor['Pages'];

beforeEach(() => {
editor = new Editor({ pageManager: true });
editor = new Editor({ pageManager: {} });
em = editor.getModel();
domc = em.get('DomComponents');
pm = em.get('PageManager');
domc = em.Components;
pm = em.Pages;
editor.getModel().loadOnStart();
initCmpLen = Object.keys(domc.allById()).length;
});

afterEach(() => {
editor.destroy();
pm = 0;
em = 0;
domc = 0;
});

test('Add page', () => {
Expand Down Expand Up @@ -203,8 +198,8 @@ describe('Managing pages', () => {
test('Remove page', () => {
const eventRm = jest.fn();
em.on(pm.events.remove, eventRm);
const page = pm.add({});
pm.remove(page.id);
const page = pm.add({})!;
pm.remove(`${page.id}`);
expect(pm.getAll().length).toBe(1);
expect(eventRm).toBeCalledTimes(1);
});
Expand All @@ -213,8 +208,8 @@ describe('Managing pages', () => {
em.on(pm.events.removeBefore, (p, c, opts) => {
opts.abort = 1;
});
const page = pm.add({});
pm.remove(page.id);
const page = pm.add({})!;
pm.remove(`${page.id}`);
expect(pm.getAll().length).toBe(2);
});

Expand All @@ -223,15 +218,15 @@ describe('Managing pages', () => {
opts.abort = 1;
complete();
});
const page = pm.add({});
pm.remove(page.id);
const page = pm.add({})!;
pm.remove(`${page.id}`);
expect(pm.getAll().length).toBe(1);
});

test('Change page', () => {
const event = jest.fn();
em.on(pm.events.update, event);
const page = pm.add({});
const page = pm.add({})!;
const up = { name: 'Test' };
const opts = { myopts: 1 };
page.set(up, opts);
Expand Down

0 comments on commit 4f3af74

Please sign in to comment.