-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(): Actually allow for custom properties that look like objects (#…
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ClassRegistry } from './ClassRegistry'; | ||
|
||
describe('ClassRegistry', () => { | ||
let classRegistry: ClassRegistry; | ||
beforeEach(() => { | ||
classRegistry = new ClassRegistry(); | ||
}); | ||
it('will error if a class is request that is not registered', () => { | ||
expect(() => classRegistry.getClass('any')).toThrow( | ||
'No class registered for any' | ||
); | ||
}); | ||
it('will return a class previously registered', () => { | ||
classRegistry.setClass(Set, 'any'); | ||
expect(classRegistry.getClass('any')).toBe(Set); | ||
}); | ||
it('will check if a class was previously registered', () => { | ||
expect(classRegistry.has('any')).toBe(false); | ||
classRegistry.setClass(Set, 'any'); | ||
expect(classRegistry.has('any')).toBe(true); | ||
}); | ||
it('not specified will register the class using the type static prop', () => { | ||
class Set2 extends Set { | ||
static type = 'SETABC'; | ||
} | ||
classRegistry.setClass(Set2); | ||
expect(classRegistry.has('SETABC')).toBe(true); | ||
expect(classRegistry.getClass('SETABC')).toBe(Set2); | ||
expect(classRegistry.getClass('setabc')).toBe(Set2); | ||
}); | ||
it('has a method for SVG parsing classes', () => { | ||
class Set2 extends Set { | ||
static type = 'SETABC'; | ||
} | ||
classRegistry.setSVGClass(Set2); | ||
expect(classRegistry.getSVGClass('SETABC')).toBe(undefined); | ||
expect(classRegistry.getSVGClass('setabc')).toBe(Set2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { enlivenObjects } from './objectEnlive'; | ||
import { Rect, type RectProps } from '../../shapes/Rect'; | ||
import { Shadow } from '../../Shadow'; | ||
import { classRegistry } from '../../ClassRegistry'; | ||
|
||
const mockedRectWithCustomProperty = { | ||
type: 'rect', | ||
width: 100, | ||
// will become a shadow | ||
shadow: { | ||
type: 'shadow', | ||
blur: 5, | ||
}, | ||
// will become a rect | ||
custom1: { | ||
type: 'rect', | ||
width: 50, | ||
}, | ||
custom2: { | ||
type: 'nothing', | ||
value: 3, | ||
}, | ||
// will become a set | ||
custom3: { | ||
type: 'registered', | ||
}, | ||
}; | ||
|
||
describe('enlivenObjects', () => { | ||
it('will enlive correctly', async () => { | ||
const [rect] = await enlivenObjects<Rect<RectProps>>([ | ||
mockedRectWithCustomProperty, | ||
]); | ||
expect(rect).toBeInstanceOf(Rect); | ||
expect(rect.shadow).toBeInstanceOf(Shadow); | ||
expect(rect.custom1).toBeInstanceOf(Rect); | ||
expect(rect.custom2).toEqual({ | ||
type: 'nothing', | ||
value: 3, | ||
}); | ||
expect(rect.custom3).toEqual({ | ||
type: 'registered', | ||
}); | ||
}); | ||
it('will enlive correctly newly registered props', async () => { | ||
class Test { | ||
declare opts: any; | ||
constructor(opts: any) { | ||
this.opts = opts; | ||
} | ||
static async fromObject(opts: any) { | ||
return new this(opts); | ||
} | ||
} | ||
classRegistry.setClass(Test, 'registered'); | ||
const [rect] = await enlivenObjects<Rect<RectProps>>([ | ||
mockedRectWithCustomProperty, | ||
]); | ||
expect(rect).toBeInstanceOf(Rect); | ||
expect(rect.shadow).toBeInstanceOf(Shadow); | ||
expect(rect.custom1).toBeInstanceOf(Rect); | ||
expect(rect.custom2).toEqual({ | ||
type: 'nothing', | ||
value: 3, | ||
}); | ||
expect(rect.custom3).toBeInstanceOf(Test); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters