|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { Schema } from '../../src/model/schema'; |
| 3 | + |
| 4 | +describe('Schema', () => { |
| 5 | + it('should be created with a valid spec', () => { |
| 6 | + expect( |
| 7 | + new Schema({ |
| 8 | + root: { children: 'para*' }, |
| 9 | + para: { children: 'text*' }, |
| 10 | + text: {}, |
| 11 | + }), |
| 12 | + ).toBeDefined(); |
| 13 | + |
| 14 | + expect(() => { |
| 15 | + new Schema({ para: { children: 'text*' } }); |
| 16 | + }).toThrowError('root node not defined'); |
| 17 | + |
| 18 | + expect(() => { |
| 19 | + new Schema({ |
| 20 | + root: { children: 'paragraph*' }, |
| 21 | + para: { children: 'text*' }, |
| 22 | + }); |
| 23 | + }).toThrowError('invalid node type: paragraph'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should build a node from JSON', () => { |
| 27 | + const schema = new Schema({ |
| 28 | + root: { children: 'para*' }, |
| 29 | + para: { children: 'text*' }, |
| 30 | + text: {}, |
| 31 | + }); |
| 32 | + |
| 33 | + expect( |
| 34 | + schema.fromJSON({ |
| 35 | + type: 'para', |
| 36 | + children: [{ type: 'text', text: 'Hello, world!' }], |
| 37 | + }), |
| 38 | + ).toEqual({ |
| 39 | + type: 'para', |
| 40 | + children: [{ type: 'text', text: 'Hello, world!' }], |
| 41 | + }); |
| 42 | + |
| 43 | + expect(() => { |
| 44 | + schema.fromJSON({ |
| 45 | + type: 'paragraph', |
| 46 | + children: [{ type: 'invalid', text: 'Hello, world!' }], |
| 47 | + }); |
| 48 | + }).toThrowError('invalid node type: paragraph'); |
| 49 | + }); |
| 50 | +}); |
0 commit comments