Skip to content

Commit

Permalink
refactor: 💡 swap order for canvas and declaration args in Slide
Browse files Browse the repository at this point in the history
Slide constructor accepted canvas argument at first place and then slide
declaration. But, custom canvas is passed rarely, more often slide
declaration is passed. So makes sense to swap them and make a slide
declaration at first place in arguments and only then canvas.

BREAKING CHANGE: 🧨 Slide constructor() has changed its signature from (canvas, declaration)
to (declaration, canvas)
  • Loading branch information
ghaiklor committed May 9, 2020
1 parent 387c15f commit 3de450f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
40 changes: 20 additions & 20 deletions packages/kittik-slide/spec/Slide.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ describe('slide', () => {
const canvas = new Canvas();

expect(() => new Slide(
canvas,
{
name: 'Test',

Expand All @@ -94,7 +93,8 @@ describe('slide', () => {
// @ts-expect-error
shapes: [{ name: 'Test', type: 'unknown' as const }],
order: [{ shape: 'Test' }]
}
},
canvas
)).toThrow(
'You have specified a shape with the name "Test" in slide "Test". ' +
'This shape has an unknown type "unknown". ' +
Expand All @@ -108,7 +108,6 @@ describe('slide', () => {
const canvas = new Canvas();

expect(() => new Slide(
canvas,
{
name: 'Test',
shapes: [{ name: 'Test', type: 'Text' as const, options: {} }],
Expand All @@ -119,7 +118,8 @@ describe('slide', () => {
// @ts-expect-error
animations: [{ name: 'Test', type: 'unknown' as const }],
order: [{ shape: 'Test' }]
}
},
canvas
)).toThrow(
'You have specified an animation with the name "Test" in slide "Test". ' +
'This animation has an unknown type "unknown". ' +
Expand All @@ -131,11 +131,11 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = new Slide(canvas, {
const slide = new Slide({
name: 'Test',
shapes: [],
order: [{ shape: 'Not Exists' }]
});
}, canvas);

await expect(slide.render()).rejects.toThrow(
'You specified shape "Not Exists" in slide "Test" as part of ordering, ' +
Expand All @@ -148,7 +148,7 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = Slide.create(canvas, SLIDE_DECLARATION);
const slide = Slide.create(SLIDE_DECLARATION, canvas);
const writeSpy = jest.spyOn(canvas, 'write').mockReturnThis();
const eraseScreenSpy = jest.spyOn(canvas, 'eraseScreen').mockReturnThis();
const flushSpy = jest.spyOn(canvas, 'flush').mockReturnThis();
Expand All @@ -164,11 +164,11 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = Slide.create(canvas, {
const slide = Slide.create({
name: 'Test',
shapes: [{ name: 'Test', type: 'Text' as const, options: {} }],
order: [{ shape: 'Test' }]
});
}, canvas);

expect(await slide.render()).toBeUndefined();
});
Expand All @@ -177,11 +177,11 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = Slide.create(canvas, {
const slide = Slide.create({
name: 'Test',
shapes: [{ name: 'Test', type: 'Text' as const, options: {} }],
order: [{ shape: 'Test', animations: ['Not Exists'] }]
});
}, canvas);

expect(await slide.render()).toBeUndefined();
});
Expand All @@ -190,7 +190,7 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = Slide.create(canvas, SLIDE_DECLARATION);
const slide = Slide.create(SLIDE_DECLARATION, canvas);

expect(slide.toObject()).toStrictEqual(SERIALIZED_SLIDE_DECLARATION);
});
Expand All @@ -199,7 +199,7 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = Slide.create(canvas, SLIDE_DECLARATION);
const slide = Slide.create(SLIDE_DECLARATION, canvas);

expect(JSON.parse(slide.toJSON())).toStrictEqual(SERIALIZED_SLIDE_DECLARATION);
});
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = new Slide(canvas, { name: 'Test', order: [], shapes: [] });
const slide = new Slide({ name: 'Test', order: [], shapes: [] }, canvas);
expect(slide.canvas).toBeInstanceOf(Canvas);
expect(slide.shapes.size).toBe(0);
expect(slide.animations.size).toBe(0);
Expand All @@ -248,12 +248,12 @@ describe('slide', () => {

const canvas = new Canvas();
const slide = new Slide(
canvas,
{
name: 'Test',
shapes: [{ name: 'Test', type: 'Text' as const, options: {} }],
order: []
}
},
canvas
);

expect(() => slide.addShape('Test', Text.create())).toThrow(
Expand All @@ -266,12 +266,12 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = new Slide(canvas, {
const slide = new Slide({
name: 'Test',
shapes: [],
order: [],
animations: [{ name: 'Test', type: 'Print' as const, options: {} }]
});
}, canvas);

expect(() => slide.addAnimation('Test', Print.create({}))).toThrow(
'You are trying to add animation with the name "Test" into the slide "Test". ' +
Expand All @@ -283,7 +283,7 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = new Slide(canvas, { name: 'Test', shapes: [], order: [{ shape: 'Test' }] });
const slide = new Slide({ name: 'Test', shapes: [], order: [{ shape: 'Test' }] }, canvas);

expect(() => slide.addOrder('Test')).toThrow(
'You already have specified an ordering for shape "Test" in slide "Test". ' +
Expand All @@ -296,7 +296,7 @@ describe('slide', () => {
expect.hasAssertions();

const canvas = new Canvas();
const slide = new Slide(canvas, { name: 'Test', shapes: [], order: [{ shape: 'Test' }] });
const slide = new Slide({ name: 'Test', shapes: [], order: [{ shape: 'Test' }] }, canvas);

expect(() => slide.addOrder('Test #2', ['Not Existing'])).toThrow(
'You have provided animations for the shape "Test #2" in slide "Test". ' +
Expand Down
8 changes: 4 additions & 4 deletions packages/kittik-slide/src/slide/Slide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Slide {
public readonly animations: Map<string, Animationable> = new Map<string, Animationable>();
public readonly order: OrderDeclaration[] = [];

public constructor (canvas?: Canvas, declaration?: SlideDeclaration) {
public constructor (declaration?: SlideDeclaration, canvas?: Canvas) {
if (typeof canvas !== 'undefined') {
this.canvas = canvas;
}
Expand All @@ -47,12 +47,12 @@ export class Slide {
}
}

public static create (canvas?: Canvas, declaration?: SlideDeclaration): Slide {
return new this(canvas, declaration);
public static create (declaration?: SlideDeclaration, canvas?: Canvas): Slide {
return new this(declaration, canvas);
}

public static fromObject (obj: SlideDeclaration, canvas?: Canvas): Slide {
return this.create(canvas, obj);
return this.create(obj, canvas);
}

public static fromJSON (json: string, canvas?: Canvas): Slide {
Expand Down

0 comments on commit 3de450f

Please sign in to comment.