| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.dm.DocumentStore' ); |
| 8 |
|
| 9 |
QUnit.test( 'Create', ( assert ) => { |
| 10 |
const done = assert.async(), |
| 11 |
fakeMongo = new ve.dm.FakeMongo(), |
| 12 |
documentStore = new ve.dm.DocumentStore( fakeMongo, 'test', fakeMongo ); |
| 13 |
|
| 14 |
documentStore.connect().then( () => documentStore.dropDatabase() ).then( () => documentStore.load( 'Foo' ) ).then( ( change ) => { |
| 15 |
assert.deepEqual( |
| 16 |
change.toJSON(), |
| 17 |
{ start: 0, transactions: [] }, |
| 18 |
'Load new empty document' |
| 19 |
); |
| 20 |
return documentStore.onNewChange( 'Foo', ve.dm.Change.static.deserialize( { |
| 21 |
start: 0, |
| 22 |
transactions: [ [ [ '', 'W' ] ], 'o', 'rld' ] |
| 23 |
}, true ) ); |
| 24 |
} ).then( () => documentStore.onNewChange( 'Foo', ve.dm.Change.static.deserialize( { |
| 25 |
start: 3, |
| 26 |
transactions: [ [ [ '', 'H' ], 5 ], 'e', 'l', 'l', 'o', ' ' ] |
| 27 |
}, true ) ) ).then( () => documentStore.load( 'Bar' ) ).then( ( change ) => { |
| 28 |
assert.deepEqual( |
| 29 |
change.toJSON(), |
| 30 |
{ start: 0, transactions: [] }, |
| 31 |
'Different document is empty' |
| 32 |
); |
| 33 |
return documentStore.onNewChange( 'Bar', ve.dm.Change.static.deserialize( { |
| 34 |
start: 44, |
| 35 |
transactions: [ [ [ '', 'X' ] ] ] |
| 36 |
}, true ) ).then( () => { |
| 37 |
assert.true( false, 'Throw on unmached start' ); |
| 38 |
} ).catch( () => { |
| 39 |
assert.true( true, 'Throw on unmatched start' ); |
| 40 |
} ); |
| 41 |
} ).then( () => documentStore.load( 'Foo' ) ).then( ( change ) => { |
| 42 |
assert.deepEqual( |
| 43 |
change.toJSON(), |
| 44 |
{ start: 0, transactions: [ [ [ '', 'W' ] ], 'o', 'rld', [ [ '', 'H' ], 5 ], 'e', 'l', 'l', 'o', ' ' ] }, |
| 45 |
'Transactions were saved' |
| 46 |
); |
| 47 |
return documentStore.dropDatabase(); |
| 48 |
} ).then( () => documentStore.load( 'Foo' ) ).then( ( change ) => { |
| 49 |
assert.deepEqual( |
| 50 |
change.toJSON(), |
| 51 |
{ start: 0, transactions: [] }, |
| 52 |
'Reload new empty document after dropDatabase' |
| 53 |
); |
| 54 |
} ).then( () => documentStore.onClose() ).then( () => { |
| 55 |
assert.deepEqual( |
| 56 |
fakeMongo.log, |
| 57 |
[ |
| 58 |
{ type: 'DocumentStore#connected', dbName: 'test' }, |
| 59 |
{ type: 'DocumentStore#dropDatabase', dbName: 'test' }, |
| 60 |
{ type: 'DocumentStore#loaded', docName: 'Foo', length: 0 }, |
| 61 |
{ type: 'DocumentStore#onNewChange', docName: 'Foo', start: 0, length: 3 }, |
| 62 |
{ type: 'DocumentStore#onNewChange', docName: 'Foo', start: 3, length: 6 }, |
| 63 |
{ type: 'DocumentStore#loaded', docName: 'Bar', length: 0 }, |
| 64 |
{ type: 'DocumentStore#loaded', docName: 'Foo', length: 9 }, |
| 65 |
{ type: 'DocumentStore#dropDatabase', dbName: 'test' }, |
| 66 |
{ type: 'DocumentStore#loaded', docName: 'Foo', length: 0 }, |
| 67 |
{ type: 'DocumentStore#onClose' } |
| 68 |
], |
| 69 |
'Log is correct' |
| 70 |
); |
| 71 |
} ).catch( ( error ) => { |
| 72 |
assert.true( false, 'Test failure: ' + error ); |
| 73 |
} ).then( () => done() ); |
| 74 |
} ); |