@@ -2,8 +2,10 @@ var async = require('async');
22var expect = require ( 'expect.js' ) ;
33var types = require ( '../../lib/types' ) ;
44var deserializedType = require ( './deserialized-type' ) ;
5+ var otRichText = require ( '@teamwork/ot-rich-text' ) ;
56types . register ( deserializedType . type ) ;
67types . register ( deserializedType . type2 ) ;
8+ types . register ( otRichText . type ) ;
79
810module . exports = function ( ) {
911describe ( 'client submit' , function ( ) {
@@ -1056,6 +1058,227 @@ describe('client submit', function() {
10561058 } ) ;
10571059 } ) ;
10581060
1061+ it ( 'does not skip processing when submitting a no-op by default' , function ( done ) {
1062+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1063+ doc . on ( 'op' , function ( ) {
1064+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'test' ) ] ) ;
1065+ done ( ) ;
1066+ } ) ;
1067+ doc . create ( [ otRichText . Action . createInsertText ( 'test' ) ] , otRichText . type . uri ) ;
1068+ doc . submitOp ( [ ] ) ;
1069+ } ) ;
1070+
1071+ it ( 'does not skip processing when submitting an identical snapshot by default' , function ( done ) {
1072+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1073+ doc . on ( 'op' , function ( ) {
1074+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'test' ) ] ) ;
1075+ done ( ) ;
1076+ } ) ;
1077+ doc . create ( [ otRichText . Action . createInsertText ( 'test' ) ] , otRichText . type . uri ) ;
1078+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'test' ) ] ) ;
1079+ } ) ;
1080+
1081+ it ( 'skips processing when submitting a no-op (no callback)' , function ( done ) {
1082+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1083+ doc . on ( 'op' , function ( ) {
1084+ done ( new Error ( 'Should not emit `op`' ) ) ;
1085+ } ) ;
1086+ doc . create ( [ otRichText . Action . createInsertText ( 'test' ) ] , otRichText . type . uri ) ;
1087+ doc . submitOp ( [ ] , { skipNoop : true } ) ;
1088+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'test' ) ] ) ;
1089+ done ( ) ;
1090+ } ) ;
1091+
1092+ it ( 'skips processing when submitting a no-op (with callback)' , function ( done ) {
1093+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1094+ doc . on ( 'op' , function ( ) {
1095+ done ( new Error ( 'Should not emit `op`' ) ) ;
1096+ } ) ;
1097+ doc . create ( [ otRichText . Action . createInsertText ( 'test' ) ] , otRichText . type . uri ) ;
1098+ doc . submitOp ( [ ] , { skipNoop : true } , done ) ;
1099+ } ) ;
1100+
1101+ it ( 'skips processing when submitting an identical snapshot (no callback)' , function ( done ) {
1102+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1103+ doc . on ( 'op' , function ( ) {
1104+ done ( new Error ( 'Should not emit `op`' ) ) ;
1105+ } ) ;
1106+ doc . create ( [ otRichText . Action . createInsertText ( 'test' ) ] , otRichText . type . uri ) ;
1107+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'test' ) ] , { skipNoop : true } ) ;
1108+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'test' ) ] ) ;
1109+ done ( ) ;
1110+ } ) ;
1111+
1112+ it ( 'skips processing when submitting an identical snapshot (with callback)' , function ( done ) {
1113+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1114+ doc . on ( 'op' , function ( ) {
1115+ done ( new Error ( 'Should not emit `op`' ) ) ;
1116+ } ) ;
1117+ doc . create ( [ otRichText . Action . createInsertText ( 'test' ) ] , otRichText . type . uri ) ;
1118+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'test' ) ] , { skipNoop : true } , done ) ;
1119+ } ) ;
1120+
1121+ it ( 'submits a snapshot when document is not created (no callback, no options)' , function ( done ) {
1122+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1123+ doc . on ( 'error' , function ( error ) {
1124+ expect ( error ) . to . be . an ( Error ) ;
1125+ expect ( error . code ) . to . equal ( 4015 ) ;
1126+ done ( ) ;
1127+ } ) ;
1128+ doc . submitSnapshot ( 7 ) ;
1129+ } ) ;
1130+
1131+ it ( 'submits a snapshot when document is not created (no callback, with options)' , function ( done ) {
1132+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1133+ doc . on ( 'error' , function ( error ) {
1134+ expect ( error ) . to . be . an ( Error ) ;
1135+ expect ( error . code ) . to . equal ( 4015 ) ;
1136+ done ( ) ;
1137+ } ) ;
1138+ doc . submitSnapshot ( 7 , { source : 'test' } ) ;
1139+ } ) ;
1140+
1141+ it ( 'submits a snapshot when document is not created (with callback, no options)' , function ( done ) {
1142+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1143+ doc . on ( 'error' , done ) ;
1144+ doc . submitSnapshot ( 7 , function ( error ) {
1145+ expect ( error ) . to . be . an ( Error ) ;
1146+ expect ( error . code ) . to . equal ( 4015 ) ;
1147+ done ( ) ;
1148+ } ) ;
1149+ } ) ;
1150+
1151+ it ( 'submits a snapshot when document is not created (with callback, with options)' , function ( done ) {
1152+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1153+ doc . on ( 'error' , done ) ;
1154+ doc . submitSnapshot ( 7 , { source : 'test' } , function ( error ) {
1155+ expect ( error ) . to . be . an ( Error ) ;
1156+ expect ( error . code ) . to . equal ( 4015 ) ;
1157+ done ( ) ;
1158+ } ) ;
1159+ } ) ;
1160+
1161+ it ( 'submits a snapshot with source (no callback)' , function ( done ) {
1162+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1163+ doc . on ( 'op' , function ( op , source ) {
1164+ expect ( op ) . to . eql ( [ otRichText . Action . createInsertText ( 'abc' ) ] ) ;
1165+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1166+ expect ( source ) . to . equal ( 'test' ) ;
1167+ done ( ) ;
1168+ } ) ;
1169+ doc . create ( [ otRichText . Action . createInsertText ( 'def' ) ] , otRichText . type . uri ) ;
1170+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] , { source : 'test' } ) ;
1171+ } ) ;
1172+
1173+ it ( 'submits a snapshot with source (with callback)' , function ( done ) {
1174+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1175+ var opEmitted = false ;
1176+ doc . on ( 'op' , function ( op , source ) {
1177+ expect ( op ) . to . eql ( [ otRichText . Action . createInsertText ( 'abc' ) ] ) ;
1178+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1179+ expect ( source ) . to . equal ( 'test' ) ;
1180+ opEmitted = true ;
1181+ } ) ;
1182+ doc . create ( [ otRichText . Action . createInsertText ( 'def' ) ] , otRichText . type . uri ) ;
1183+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] , { source : 'test' } , function ( error ) {
1184+ expect ( opEmitted ) . to . equal ( true ) ;
1185+ done ( error ) ;
1186+ } ) ;
1187+ } ) ;
1188+
1189+ it ( 'submits a snapshot without source (no callback)' , function ( done ) {
1190+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1191+ doc . on ( 'op' , function ( op , source ) {
1192+ expect ( op ) . to . eql ( [ otRichText . Action . createInsertText ( 'abc' ) ] ) ;
1193+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1194+ expect ( source ) . to . equal ( true ) ;
1195+ done ( ) ;
1196+ } ) ;
1197+ doc . create ( [ otRichText . Action . createInsertText ( 'def' ) ] , otRichText . type . uri ) ;
1198+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1199+ } ) ;
1200+
1201+ it ( 'submits a snapshot without source (with callback)' , function ( done ) {
1202+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1203+ var opEmitted = false ;
1204+ doc . on ( 'op' , function ( op , source ) {
1205+ expect ( op ) . to . eql ( [ otRichText . Action . createInsertText ( 'abc' ) ] ) ;
1206+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1207+ expect ( source ) . to . equal ( true ) ;
1208+ opEmitted = true ;
1209+ } ) ;
1210+ doc . create ( [ otRichText . Action . createInsertText ( 'def' ) ] , otRichText . type . uri ) ;
1211+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] , function ( error ) {
1212+ expect ( opEmitted ) . to . equal ( true ) ;
1213+ done ( error ) ;
1214+ } ) ;
1215+ } ) ;
1216+
1217+ it ( 'submits a snapshot and syncs it' , function ( done ) {
1218+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1219+ var doc2 = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1220+ doc2 . on ( 'create' , function ( ) {
1221+ doc2 . submitSnapshot ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1222+ } ) ;
1223+ doc . on ( 'op' , function ( op , source ) {
1224+ expect ( op ) . to . eql ( [ otRichText . Action . createInsertText ( 'abc' ) ] ) ;
1225+ expect ( source ) . to . equal ( false ) ;
1226+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'abcdef' ) ] ) ;
1227+ done ( ) ;
1228+ } ) ;
1229+ doc . subscribe ( function ( err ) {
1230+ if ( err ) return done ( err ) ;
1231+ doc2 . subscribe ( function ( err ) {
1232+ if ( err ) return done ( err ) ;
1233+ doc . create ( [ otRichText . Action . createInsertText ( 'def' ) ] , otRichText . type . uri ) ;
1234+ } ) ;
1235+ } ) ;
1236+ } ) ;
1237+
1238+ it ( 'submits a snapshot (no diff, no diffX, no callback)' , function ( done ) {
1239+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1240+ doc . on ( 'error' , function ( error ) {
1241+ expect ( error ) . to . be . an ( Error ) ;
1242+ expect ( error . code ) . to . equal ( 4024 ) ;
1243+ done ( ) ;
1244+ } ) ;
1245+ doc . create ( { test : 5 } ) ;
1246+ doc . submitSnapshot ( { test : 7 } ) ;
1247+ } ) ;
1248+
1249+ it ( 'submits a snapshot (no diff, no diffX, with callback)' , function ( done ) {
1250+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1251+ doc . on ( 'error' , done ) ;
1252+ doc . create ( { test : 5 } ) ;
1253+ doc . submitSnapshot ( { test : 7 } , function ( error ) {
1254+ expect ( error ) . to . be . an ( Error ) ;
1255+ expect ( error . code ) . to . equal ( 4024 ) ;
1256+ done ( ) ;
1257+ } ) ;
1258+ } ) ;
1259+
1260+ it ( 'submits a snapshot without a diffHint' , function ( done ) {
1261+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1262+ doc . create ( [ otRichText . Action . createInsertText ( 'aaaa' ) ] , otRichText . type . uri ) ;
1263+ doc . on ( 'op' , function ( op ) {
1264+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'aaaaa' ) ] ) ;
1265+ expect ( op ) . to . eql ( [ otRichText . Action . createInsertText ( 'a' ) ] ) ;
1266+ done ( ) ;
1267+ } ) ;
1268+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'aaaaa' ) ] ) ;
1269+ } ) ;
1270+
1271+ it ( 'submits a snapshot with a diffHint' , function ( done ) {
1272+ var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
1273+ doc . create ( [ otRichText . Action . createInsertText ( 'aaaa' ) ] , otRichText . type . uri ) ;
1274+ doc . on ( 'op' , function ( op ) {
1275+ expect ( doc . data ) . to . eql ( [ otRichText . Action . createInsertText ( 'aaaaa' ) ] ) ;
1276+ expect ( op ) . to . eql ( [ otRichText . Action . createRetain ( 2 ) , otRichText . Action . createInsertText ( 'a' ) ] ) ;
1277+ done ( ) ;
1278+ } ) ;
1279+ doc . submitSnapshot ( [ otRichText . Action . createInsertText ( 'aaaaa' ) ] , { diffHint : 2 } ) ;
1280+ } ) ;
1281+
10591282 describe ( 'type.deserialize' , function ( ) {
10601283 it ( 'can create a new doc' , function ( done ) {
10611284 var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
0 commit comments