Skip to content

Commit cc071a2

Browse files
committed
feat: add ci and op convert insert test
1 parent 65bb9b1 commit cc071a2

10 files changed

Lines changed: 101 additions & 8 deletions

File tree

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
3+
node_js:
4+
- '12'
5+
6+
env:
7+
- workerCount=3 timeout=600000
8+
9+
script:
10+
- npm run test
11+
12+
install:
13+
- npm run bootstrap
14+
15+
cache:
16+
directories:
17+
- node_modules

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"build": "lerna run build --stream",
1010
"watch": "lerna run --parallel watch",
1111
"prebuild": "rm -rf ./packages/**/lib/",
12+
"test": "lerna run test --stream",
1213
"format": "prettier --write"
1314
},
1415
"workspaces": [

packages/bridge/package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"build": "yarn run build:types && yarn run build:js",
2222
"build:types": "tsc --emitDeclarationOnly",
2323
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline",
24-
"watch": "yarn build:js -w"
24+
"watch": "yarn build:js -w",
25+
"test": "jest"
2526
},
2627
"dependencies": {
2728
"typescript": "^3.6.3"
@@ -32,7 +33,10 @@
3233
"@babel/plugin-proposal-class-properties": "^7.5.5",
3334
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
3435
"@babel/preset-env": "^7.6.0",
35-
"@babel/preset-typescript": "^7.6.0"
36+
"@babel/preset-typescript": "^7.6.0",
37+
"@types/jest": "^24.0.19",
38+
"jest": "^24.9.0",
39+
"ts-jest": "^24.1.0"
3640
},
3741
"directories": {
3842
"lib": "lib"
@@ -41,5 +45,14 @@
4145
"slate",
4246
"automerge",
4347
"bridge"
44-
]
48+
],
49+
"jest": {
50+
"roots": [
51+
"<rootDir>/src"
52+
],
53+
"transform": {
54+
"^.+\\.ts?$": "ts-jest"
55+
},
56+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$"
57+
}
4558
}

packages/bridge/src/apply/annotation.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { Operation, SyncDoc } from '../model/index'
22

3+
// TODO: handle annotation ops
4+
35
export const addAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
4-
console.log('addAnnotation!!!', op.toJS())
6+
// console.log('addAnnotation!!!', op.toJS())
57
return doc
68
}
79

810
export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
9-
console.log('removeAnnotation!!!', op.toJS())
11+
// console.log('removeAnnotation!!!', op.toJS())
1012
return doc
1113
}
1214

1315
export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
14-
console.log('setAnnotation!!!', op.toJS())
16+
// console.log('setAnnotation!!!', op.toJS())
1517
return doc
1618
}
1719

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as Automerge from 'automerge'
2+
import { toSlateOp } from './index'
3+
import { createDoc, cloneDoc, createParagraphJSON } from '../utils'
4+
5+
describe('convert operations to slatejs model', () => {
6+
it('convert insert operations', () => {
7+
const doc1 = createDoc()
8+
const doc2 = cloneDoc(doc1)
9+
10+
const change = Automerge.change(doc1, 'change', (d: any) => {
11+
d.document.nodes.push(createParagraphJSON('hello!'))
12+
d.document.nodes[1].nodes[0].text = 'hello!'
13+
})
14+
15+
const operations = Automerge.diff(doc2, change)
16+
17+
const slateOps = toSlateOp(operations, change)
18+
19+
const expectedOps = [
20+
{
21+
type: 'insert_node',
22+
path: [1],
23+
node: { object: 'block', type: 'paragraph', nodes: [] }
24+
},
25+
{
26+
type: 'insert_node',
27+
path: [1, 0],
28+
node: { object: 'text', marks: [], text: 'hello!' }
29+
}
30+
]
31+
32+
expect(slateOps).toStrictEqual(expectedOps)
33+
})
34+
})

packages/bridge/src/convert/set.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const AnnotationSetOp = ({ key, value }: Automerge.Diff) => (map, doc) => {
2020
/**
2121
* Looks like set_annotation option is broken, temporary disabled
2222
*/
23+
2324
// if (!doc.annotations[key]) {
2425
op = {
2526
type: 'add_annotation',

packages/bridge/src/cursor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const removeCursor = (doc: SyncDoc, key: CursorKey) => {
5151
return doc
5252
}
5353

54-
export const cursorOpFilter = (ops: Immutable.List<Operation>, type: string) =>
54+
export const cursorOpFilter = (ops: any, type: string): any =>
5555
ops.filter(op => {
5656
if (op.type === 'set_annotation') {
5757
return !(

packages/bridge/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import toSync from './toSync'
22
import hexGen from './hexGen'
33

4+
export * from './testUtils'
5+
46
const toJS = node => {
57
try {
68
return JSON.parse(JSON.stringify(node))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as Automerge from 'automerge'
2+
import { ValueJSON, TextJSON, NodeJSON } from 'slate'
3+
4+
export const createTextJSON = (text: string = ''): TextJSON => ({
5+
object: 'text',
6+
marks: [],
7+
text
8+
})
9+
10+
export const createParagraphJSON = (text: string = ''): NodeJSON => ({
11+
object: 'block',
12+
type: 'paragraph',
13+
nodes: [createTextJSON(text)]
14+
})
15+
16+
export const createValueJSON = (): ValueJSON => ({
17+
document: {
18+
nodes: [createParagraphJSON()]
19+
}
20+
})
21+
22+
export const createDoc = () => Automerge.from(createValueJSON())
23+
24+
export const cloneDoc = doc => Automerge.change(doc, '', d => d)

packages/example/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
},
2828
"scripts": {
2929
"start": "react-scripts start",
30-
"build": "react-scripts build",
3130
"dev": "concurrently \"yarn start\" \"yarn serve\"",
3231
"serve": "nodemon --watch ../backend/lib --inspect server.js"
3332
},

0 commit comments

Comments
 (0)