Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新master #1

Merged
merged 106 commits into from
Dec 5, 2019
Merged
Changes from 1 commit
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
60c8bd5
[V1 UMBRELLA] (#346)
janryWang Oct 24, 2019
afa1dcf
feat: add schema-editor
Oct 24, 2019
be68d3d
feat: rename schema-editor -> react-schema-editor
Oct 25, 2019
667c9ef
Merge pull request #347 from ascoders/v1
ascoders Oct 25, 2019
1cc755e
Fix CI and some Bugs (#348)
janryWang Oct 25, 2019
209a4da
fix(@uform/react-shared-components): fix text (#349)
janryWang Oct 25, 2019
c0922b2
[UMBRELLA V1] Compat v0 (#351)
janryWang Oct 28, 2019
fc516fb
feat(@uform/react): support filter changed (#353)
janryWang Oct 29, 2019
d71235f
[WIP]feat(core): add lifecycles unit test (#355)
JohnIsOnTheRoad Oct 31, 2019
778a020
feat(core): add graph unit test (#357)
JohnIsOnTheRoad Nov 1, 2019
985aca3
V1 fix subscribe (#358)
janryWang Nov 1, 2019
42952c2
fix(@uform/core): fix process calculation logic
outerkk Nov 1, 2019
ba5678c
fix(@uform/core): fix position of stack counter
outerkk Nov 3, 2019
e7edcfa
feat(core): add model unit test (#359)
JohnIsOnTheRoad Nov 3, 2019
86dd387
fix(@uform/core): fix visible/display behave
outerkk Nov 3, 2019
db68c8d
Merge branch 'v1' of github.com:alibaba/uform into v1_fix_process
outerkk Nov 3, 2019
696bcc6
refactor(@uform/core): merge v1
outerkk Nov 3, 2019
56835f9
refactor(@uform/core): remove processing test case
outerkk Nov 3, 2019
95ee596
fix(@uform/core): fix process calculation logic (#362)
janryWang Nov 3, 2019
c3e42d4
test(@uform/react-schema-renderer): add old test case
Nov 3, 2019
51c0499
test(@uform/react-schema-renderer): add old test case
Nov 3, 2019
df89aa3
Merge pull request #364 from ZirkleTsing/v1
janryWang Nov 3, 2019
e933304
feat: onFieldChange types
Nov 4, 2019
dc4fa80
fix: [onFieldChange] types
Nov 4, 2019
6421b7f
test(@uform/react-schema-renderer): fix test case uncompat bugs (#369)
janryWang Nov 5, 2019
579a91e
V1 (#370)
ZirkleTsing Nov 5, 2019
ff454e9
test(@uform/react-schema-renderer): fix test case uncompat bugs (#373)
janryWang Nov 6, 2019
9cd7272
feat(shared): add unit test (#374)
s0ngyee Nov 6, 2019
dfec008
refactor: improve test case (#375)
janryWang Nov 6, 2019
cf3787b
docs(@uform/core): add docs (#376)
janryWang Nov 7, 2019
4377180
feat: add silent option (#377)
JohnIsOnTheRoad Nov 7, 2019
3ec4f09
feat: add actions test
Nov 7, 2019
c806577
Merge branch 'v1' into v1
Nov 7, 2019
e7e5a80
Merge pull request #378 from jincdream/v1
Nov 7, 2019
a41315a
Revert "V1" (#379)
janryWang Nov 7, 2019
64c527c
Feat support watch (#382)
janryWang Nov 8, 2019
fe4ce56
test(@uform/react-schema-renderer): add old test case (#381)
ZirkleTsing Nov 9, 2019
c69f71a
test(@uform/react-schema-renderer): improve v0 test cases (#384)
janryWang Nov 10, 2019
7a93b73
feat: support formStep component (#385)
janryWang Nov 10, 2019
e49d7be
Fix next item style (#388)
janryWang Nov 11, 2019
646203c
test(@uform/react-schema-renderer): add old test case (#391)
ZirkleTsing Nov 12, 2019
b985265
test(@uform/react-schema-renderer): Compat old test (#392)
janryWang Nov 13, 2019
21ee40b
feat: add react/actions tests
anyuxuan Nov 14, 2019
fa7bae7
feature(@uform/react): add useForm unittest (#393)
JohnIsOnTheRoad Nov 14, 2019
ecff8ef
feat: add docs and some test cases (#395)
janryWang Nov 14, 2019
57ecf3e
docs: improve docs (#396)
janryWang Nov 14, 2019
88fd3eb
Fix docs typo (#397)
janryWang Nov 14, 2019
66e03bc
fix(@uform/antd/next): fix typings (#398)
janryWang Nov 14, 2019
4cf4bb8
feature(@uform/react): finish hook unittest (#400)
JohnIsOnTheRoad Nov 16, 2019
ded9b93
docs(@uform/core): improve docs (#402)
janryWang Nov 19, 2019
570a81c
[WIP]doc(@uform/react): add react doc (#401)
JohnIsOnTheRoad Nov 19, 2019
dff959c
feat: support useFormEffects (#403)
janryWang Nov 19, 2019
7b73255
fix(@uform/core): fix required (#404)
janryWang Nov 20, 2019
70a5b67
docs: fix typo and improve docs (#405)
yujiangshui Nov 21, 2019
7fe8a78
docs(@uform/react): improve docs (#406)
JohnIsOnTheRoad Nov 21, 2019
193b3e3
docs: fix typo and improve docs (#408)
JohnIsOnTheRoad Nov 22, 2019
e17ebae
Fix isFn can't test async function (#411)
javahuang Nov 22, 2019
b5400ef
Improve docs (#407)
janryWang Nov 22, 2019
8dbffb8
Improve docs (#412)
janryWang Nov 22, 2019
66dff2c
Improve docs (#413)
janryWang Nov 22, 2019
29c6846
Improve docs (#414)
janryWang Nov 22, 2019
4be1740
update readme
janryWang Nov 22, 2019
0aeeed9
update readme
janryWang Nov 22, 2019
2f56bf3
update readme
janryWang Nov 22, 2019
d5922f7
core: fix remove function of object field mutator (#410)
JohnIsOnTheRoad Nov 23, 2019
737fa54
Fix remove logics (#415)
janryWang Nov 23, 2019
0fc5f73
Improve docs (#416)
janryWang Nov 23, 2019
623e330
Fix registry (#417)
janryWang Nov 23, 2019
1e5d780
Merge branch 'master' into v1
janryWang Nov 23, 2019
79d7d36
chore(publish): v1.0.0-alpha.0
janryWang Nov 23, 2019
c2ca2ba
fix(@uform/react-shared-components): fix preview (#418)
janryWang Nov 23, 2019
204b2ad
feat(@uform/antd/next): Support error scroll (#419)
janryWang Nov 23, 2019
908882a
feat(@uform/core): support pass visible/display of register method (#…
janryWang Nov 24, 2019
670fadb
feat(@uform/react): remove raf and fix unittest (#422)
JohnIsOnTheRoad Nov 24, 2019
fbf8937
chore(publish): v1.0.0-alpha.1
janryWang Nov 24, 2019
06677ad
fix(@uform/core): fix onsubmit immutable values (#424)
janryWang Nov 24, 2019
a758a1c
Fix schema name (#425)
janryWang Nov 25, 2019
43c30a6
Improve perf (#426)
janryWang Nov 25, 2019
476d4cb
chore(publish): v1.0.0-alpha.2
janryWang Nov 25, 2019
b446c02
feat(@uform/antd/next): improve form step (#431)
janryWang Nov 25, 2019
9440ac4
feat(@uform/react): support useFieldState/useFormState (#433)
janryWang Nov 26, 2019
551d74c
feat(@uform/react): actions support clearErrors (#434)
janryWang Nov 26, 2019
19e2627
fix(@uform/antd): Fix deps (#435)
janryWang Nov 26, 2019
e2aa55e
fix(@uform/react): fix unmount (#436)
janryWang Nov 26, 2019
853e051
test(@uform/react): improve field and virtualField test cases (#438)
javahuang Nov 27, 2019
fd93386
fix(@uform/antd/next/react): doc (#437)
JohnIsOnTheRoad Nov 27, 2019
15b6b43
feat(@uform/next): update next features (#439)
janryWang Nov 27, 2019
3bfe515
fix(@uform/next): formitem compatibility (#440)
JohnIsOnTheRoad Nov 27, 2019
a6ce435
update readme
janryWang Nov 27, 2019
c0701f5
update readme
janryWang Nov 27, 2019
802d2f7
chore(publish): v1.0.0-alpha.3
janryWang Nov 27, 2019
bf14478
表单 editor 新增菜单树功能 (#441)
ascoders Nov 28, 2019
0872f40
fix(@uform/react/antd/next): Fix field state errors (#442)
javahuang Nov 28, 2019
013d427
Fix xeffect (#443)
janryWang Nov 28, 2019
8b3d633
fix(@uform/antd): fix FormItem supports className (#444)
cds803 Nov 28, 2019
15a4147
SchemaEditor MVP版 (#446)
henryybai Nov 29, 2019
2d9b4ba
refactor(readme): update
janryWang Nov 29, 2019
2353a42
chore(publish): v1.0.0-alpha.4
janryWang Nov 29, 2019
fba8e15
fix(@uform/validator): fix minLength is not work (#447)
janryWang Nov 30, 2019
745a0d9
fix: in miniapp (worker runtime) , globalThis is not a function
Dec 3, 2019
4b6a9c0
fix: in miniapp, globalSelf is existing
Dec 3, 2019
785a760
feat: json to basic schema (#450)
welkang Dec 3, 2019
4aa8e74
Fix value sync (#451)
janryWang Dec 3, 2019
25eb8dc
Improve perf (#452)
janryWang Dec 4, 2019
6768dd3
build(@uform/antd/next): fix building (#456)
janryWang Dec 5, 2019
7bf702d
chore(publish): v1.0.0-alpha.5
janryWang Dec 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(shared): add unit test (alibaba#374)
* feat(shared): add unit test

* chore(shared): remove debug code

* chore(shared): update dependencies
s0ngyee authored and janryWang committed Nov 6, 2019

Verified

This commit was signed with the committer’s verified signature.
not-an-aardvark Teddy Katz
commit 9cd727257926cdd9e3fa764481ad949dff68eb42
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -84,6 +84,7 @@
"fs-extra": "^7.0.1",
"ghooks": "^2.0.4",
"glob": "^7.1.3",
"immutable": "^4.0.0-rc.12",
"istanbul-api": "^2.1.1",
"istanbul-lib-coverage": "^2.0.3",
"jest": "^24.1.0",
@@ -95,6 +96,7 @@
"lerna": "^3.10.1",
"lint-staged": "^8.2.1",
"majo": "^0.7.1",
"moment": "^2.24.0",
"onchange": "^5.2.0",
"prettier": "^1.18.2",
"pretty-format": "^24.0.0",
407 changes: 316 additions & 91 deletions packages/shared/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,341 @@
import moment from 'moment'
import { Map as ImmutableMap } from 'immutable'
import { isEqual } from '../compare'
import { toArr, every, some, findIndex, find, includes } from '../array'
import { toArr, every, some, findIndex, find, includes, map, reduce } from '../array'
import { clone } from '../clone'
import { lowercase } from '../case'
import { deprecate } from '../deprecate'
import { isValid, isEmpty } from '../isEmpty'
import { stringLength } from '../string'
import { Subscribable } from '../subscribable'

describe('array', () => {
test('toArr', () => {
expect(isEqual(toArr([123]), [123])).toBeTruthy()
expect(isEqual(toArr(123), [123])).toBeTruthy()
expect(isEqual(toArr(null), [])).toBeTruthy()
})

test('toArr', () => {
expect(isEqual(toArr([123]), [123])).toBeTruthy()
expect(isEqual(toArr(123), [123])).toBeTruthy()
expect(isEqual(toArr(null), [])).toBeTruthy()
test('some', () => {
const values1 = [1, 2, 3, 4, 5]
const values2 = []
const values3 = { a: 1, b: 2, c: 3 }
const values4 = {}
expect(some(values1, item => item === 3)).toBeTruthy()
expect(some(values1, item => item === 6)).toBeFalsy()
expect(some(values2, () => true)).toBeFalsy()
expect(some(values2, () => false)).toBeFalsy()
expect(some(values3, item => item === 3)).toBeTruthy()
expect(some(values3, item => item === 6)).toBeFalsy()
expect(some(values4, () => true)).toBeFalsy()
expect(some(values4, () => false)).toBeFalsy()
})

test('every', () => {
const values1 = [1, 2, 3, 4, 5]
const values2 = []
const values3 = { a: 1, b: 2, c: 3 }
const values4 = {}
expect(every(values1, item => item < 6)).toBeTruthy()
expect(every(values1, item => item < 3)).toBeFalsy()
expect(every(values2, () => true)).toBeTruthy()
expect(every(values2, () => false)).toBeTruthy()
expect(every(values2, () => false)).toBeTruthy()
expect(every(values3, item => item < 6)).toBeTruthy()
expect(every(values3, item => item < 3)).toBeFalsy()
expect(every(values4, () => false)).toBeTruthy()
expect(every(values4, () => false)).toBeTruthy()
})

test('findIndex', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(findIndex(value, item => item > 3), 3)).toBeTruthy()
expect(isEqual(findIndex(value, item => item < 3, true), 1)).toBeTruthy()
expect(isEqual(findIndex(value, item => item > 6), -1)).toBeTruthy()
})

test('find', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(find(value, item => item > 3), 4)).toBeTruthy()
expect(isEqual(find(value, item => item < 3, true), 2)).toBeTruthy()
expect(isEqual(find(value, item => item > 6), void 0)).toBeTruthy()
})

test('includes', () => {
const value = [1, 2, 3, 4, 5]
expect(includes(value, 3)).toBeTruthy()
expect(includes(value, 6)).toBeFalsy()
expect(includes('some test string', 'test')).toBeTruthy()
expect(includes('some test string', 'test2')).toBeFalsy()
})

test('map', () => {
const value = [1, 2, 3, 4, 5]
const stringVal = 'some test string'
const obj = { k1: 'v1', k2: 'v2' }
expect(isEqual(map(value, item => item + 1, true), [6, 5, 4, 3, 2])).toBeTruthy()
expect(isEqual(map(stringVal, item => item), stringVal.split(''))).toBeTruthy()
expect(isEqual(map(obj, (item, key) => `${item}-copy`), { k1: 'v1-copy', k2: 'v2-copy' })).toBeTruthy()
})

test('reduce', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(reduce(value, (acc, item) => acc + item, 0, true), 15)).toBeTruthy()
})
})

test('clone form data', () => {
var dd = new Map()
dd.set('aaa', { bb: 123 })
var a = {
aa: 123123,
bb: [{ bb: 111 }, { bb: 222 }],
cc: () => {
// eslint-disable-next-line no-console
console.log('123')
},
dd
}
var cloned = clone(a)
expect(isEqual(cloned, a)).toBeTruthy()
expect(a === cloned).toBeFalsy()
expect(a.bb[0] === cloned.bb[0]).toBeFalsy()
expect(a.dd === cloned.dd).toBeFalsy()
expect(a.dd.get('aaa') === cloned.dd.get('aaa')).toBeTruthy()
expect(a.cc === cloned.cc).toBeTruthy()
describe('case', () => {
test('lowercase', () => {
expect(lowercase('SOME_UPPER_CASE_TEXT')).toEqual('some_upper_case_text')
expect(lowercase('')).toEqual('')
})
})

test('filter equal', () => {
var a = {
aa: {
bb: 123
describe('compare', () => {
// base
expect(isEqual('some test string', 'some test string')).toBeTruthy()

// array
expect(isEqual([{ k1: 'v1' }, { k2: 'v2' }], [{ k1: 'v1' }, { k2: 'v2' }])).toBeTruthy()
expect(isEqual([{ k1: 'v1' }, { k2: 'v2' }], [{ k1: 'v1' }])).toBeFalsy()
expect(isEqual([{ k1: 'v1' }, { k2: 'v2' }], [{ k1: 'v1' }], (_, key) => key !== 'k1')).toBeFalsy()

// moment
const momentA = moment('2019-11-11', 'YYYY-MM-DD')
const momentB = moment('2019-11-10', 'YYYY-MM-DD')
expect(isEqual(momentA, {})).toBeFalsy()
expect(isEqual(momentA, moment('2019-11-11', 'YYYY-MM-DD'))).toBeTruthy()
expect(isEqual(momentA, momentB)).toBeFalsy()

// immutable
const immutableA = ImmutableMap({ key: 'val' })
const immutableB = ImmutableMap({ key1: 'val1' })
expect(isEqual(immutableA, {})).toBeFalsy()
expect(isEqual(immutableA, immutableB)).toBeFalsy()
// schema
// todo
// date
const dateA = new Date('2019-11-11')
const dateB = new Date('2019-11-10')
expect(isEqual(dateA, {})).toBeFalsy()
expect(isEqual(dateA, dateB)).toBeFalsy()
expect(isEqual(dateA, new Date('2019-11-11'))).toBeTruthy()
// regexp
const regexpA = new RegExp(/test/)
const regexpB = new RegExp(/test2/)
expect(isEqual(regexpA, {})).toBeFalsy()
expect(isEqual(regexpA, new RegExp(/test/))).toBeTruthy()
expect(isEqual(regexpA, regexpB)).toBeFalsy()
// URL
const urlA = new URL('https://uformjs.org/')
const urlB = new URL('https://www.taobao.com')
const urlC = new URL('https://uformjs.org/')
expect(isEqual(urlA, urlC)).toBeTruthy()
expect(isEqual(urlA, urlB)).toBeFalsy()
// object
const objA = { key: 'val' }
const objB = { key2: 'val2', key3: 'val3' }
const objC = { key2: 'val2' }
expect(isEqual(objA, { key: 'val' })).toBeTruthy()
expect(isEqual(objA, objB)).toBeFalsy()
expect(isEqual(objA, objC)).toBeFalsy()
})

describe('clone and compare', () => {
test('clone form data', () => {
var dd = new Map()
dd.set('aaa', { bb: 123 })
// var ee = new WeakMap()
// ee.set({}, 1)
// var ff = new WeakSet()
// ff.add({})
// var gg = new Set()
// gg.add(3)

var a = {
aa: 123123,
bb: [{ bb: 111 }, { bb: 222 }],
cc: () => {
// eslint-disable-next-line no-console
console.log('123')
},
dd,
// ee,
// ff,
// gg
}
var cloned = clone(a)
expect(isEqual(cloned, a)).toBeTruthy()
expect(a === cloned).toBeFalsy()
expect(a.bb[0] === cloned.bb[0]).toBeFalsy()
expect(a.dd === cloned.dd).toBeFalsy()
expect(a.dd.get('aaa') === cloned.dd.get('aaa')).toBeTruthy()
expect(a.cc === cloned.cc).toBeTruthy()
// expect(a.ee === cloned.ee).toBeTruthy()
// expect(a.ff === cloned.ff).toBeTruthy()
// expect(a.gg === cloned.gg).toBeTruthy()
})

test('filter equal', () => {
var a = {
aa: {
bb: 123
}
}
}
var b = {
aa: {
bb: 123
var b = {
aa: {
bb: 123
}
}
}

expect(isEqual(a, b)).toBeTruthy()
expect(isEqual(a, b, (_, key) => key !== 'aa')).toBeTruthy()
})

test('filter clone', () => {
var a = {
aa: {
bb: 123
},
cc: {
dd: [1, 3, 4, 5]
}
}

var b = clone(a, (_, key) => key !== 'aa')

expect(a.aa === b.aa).toBeTruthy()
expect(a.cc === b.cc).toBeFalsy()
expect(isEqual(a.cc, b.cc)).toBeTruthy()
})
})

describe('deprecate', () => {
test('deprecate', () => {
const test = jest.fn(() => {
console.log('### deprecated function called ###')
})
const deprecatedFn = jest.fn(deprecate(test, 'Some.Deprecated.Api', 'some deprecated error'))

// arguments - function
deprecatedFn()
expect(deprecatedFn).toHaveBeenCalledTimes(1)
expect(test).toHaveBeenCalledTimes(1)

// arguments - string
const testDeprecatedFn = jest.fn(() => deprecate('Some.Deprecated.Api', 'some deprecated error'))
testDeprecatedFn()
expect(testDeprecatedFn).toHaveBeenCalledTimes(1)

expect(isEqual(a, b)).toBeTruthy()
expect(isEqual(a, b, (_, key) => key !== 'aa')).toBeTruthy()

// arguments - empty string
const testDeprecatedFn2 = jest.fn(() => deprecate('Some.Deprecated.Api'))
testDeprecatedFn2()
expect(testDeprecatedFn2).toHaveBeenCalledTimes(1)
})
})

test('filter clone', () => {
var a = {
aa: {
bb: 123
},
cc: {
dd: [1, 3, 4, 5]
describe('isEmpty', () => {
test('isValid', () => {
// val - undefined
expect(isValid(undefined)).toBeFalsy()
// val - any
expect(isValid(!undefined)).toBeTruthy()
})

test('isEmpty', () => {
// val - null
expect(isEmpty(null)).toBeTruthy()

// val - boolean
expect(isEmpty(true)).toBeFalsy()

// val - number
expect(isEmpty(2422)).toBeFalsy()

// val - string
expect(isEmpty('some text')).toBeFalsy()
expect(isEmpty('')).toBeTruthy()

// val - function
const emptyFunc = function() {}
const nonEmptyFunc = function(payload) {
console.log(payload)
}
}
expect(isEmpty(emptyFunc)).toBeTruthy()
expect(isEmpty(nonEmptyFunc)).toBeFalsy()

var b = clone(a, (_, key) => key !== 'aa')
// val - arrays
expect(isEmpty([])).toBeTruthy()
expect(isEmpty([1, 2, 3, 4, 5])).toBeFalsy()
expect(isEmpty([0, undefined, null, ''])).toBeTruthy()

expect(a.aa === b.aa).toBeTruthy()
expect(a.cc === b.cc).toBeFalsy()
expect(isEqual(a.cc, b.cc)).toBeTruthy()
})
// val - errors
expect(isEmpty(new Error())).toBeTruthy()
expect(isEmpty(new Error('some error'))).toBeFalsy()

// val - objects
expect(isEmpty(new File(['foo'], 'filename.txt', { type: 'text/plain' }))).toBeFalsy()
expect(isEmpty(new Map())).toBeTruthy()
expect(isEmpty(new Map().set('key', 'val'))).toBeFalsy()
expect(isEmpty(new Set())).toBeTruthy()
expect(isEmpty(new Set([1, 2]))).toBeFalsy()
expect(isEmpty({key: 'val'})).toBeFalsy()
expect(isEmpty({})).toBeTruthy()

test('some', () => {
const values1 = [1, 2, 3, 4, 5]
const values2 = []
const values3 = { a: 1, b: 2, c: 3 }
const values4 = {}
expect(some(values1, item => item === 3)).toBeTruthy()
expect(some(values1, item => item === 6)).toBeFalsy()
expect(some(values2, () => true)).toBeFalsy()
expect(some(values2, () => false)).toBeFalsy()
expect(some(values3, item => item === 3)).toBeTruthy()
expect(some(values3, item => item === 6)).toBeFalsy()
expect(some(values4, () => true)).toBeFalsy()
expect(some(values4, () => false)).toBeFalsy()
expect(isEmpty(Symbol()))
})
})

test('every', () => {
const values1 = [1, 2, 3, 4, 5]
const values2 = []
const values3 = { a: 1, b: 2, c: 3 }
const values4 = {}
expect(every(values1, item => item < 6)).toBeTruthy()
expect(every(values1, item => item < 3)).toBeFalsy()
expect(every(values2, () => true)).toBeTruthy()
expect(every(values2, () => false)).toBeTruthy()
expect(every(values2, () => false)).toBeTruthy()
expect(every(values3, item => item < 6)).toBeTruthy()
expect(every(values3, item => item < 3)).toBeFalsy()
expect(every(values4, () => false)).toBeTruthy()
expect(every(values4, () => false)).toBeTruthy()
describe('string', () => {
test('stringLength', () => {
expect(stringLength('🦄some text')).toEqual(10)
})
})

test('findIndex', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(findIndex(value, item => item > 3), 3)).toBeTruthy()
expect(isEqual(findIndex(value, item => item < 3, true), 1)).toBeTruthy()
expect(isEqual(findIndex(value, item => item > 6), -1)).toBeTruthy()
})
describe('shared Subscribable', () => {
test('Subscribable', () => {
const cb = jest.fn(payload => payload)

test('find', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(find(value, item => item > 3), 4)).toBeTruthy()
expect(isEqual(find(value, item => item < 3, true), 2)).toBeTruthy()
expect(isEqual(find(value, item => item > 6), void 0)).toBeTruthy()
})
// defualt subscribable
const obj = new Subscribable()
const handlerIdx = obj.subscribe(cb)
expect(handlerIdx).toEqual(1)
obj.notify({ key: 'val'})
expect(cb).toHaveBeenCalledTimes(1)
expect(cb).toBeCalledWith({ key: 'val' })

test('includes', () => {
const value = [1, 2, 3, 4, 5]
expect(includes(value, 3)).toBeTruthy()
expect(includes(value, 6)).toBeFalsy()
obj.unsubscribe(handlerIdx)
obj.notify({ key: 'val'})
expect(cb).toHaveBeenCalledTimes(1)

// subscribable with custom filter
const objWithCustomFilter = new Subscribable();
const customFilter = (payload) => {
payload.key2 = 'val2'
return payload
}
objWithCustomFilter.subscription = {
filter: customFilter
}
objWithCustomFilter.subscribe(cb)
const handlerIdx2 = objWithCustomFilter.subscribe(cb)
expect(handlerIdx2).toEqual(2)
objWithCustomFilter.notify({ key4: 'val4'})
expect(cb).toHaveBeenCalledTimes(3)
expect(cb).toBeCalledWith({ key4: 'val4', key2: 'val2' })

// subscribable with custom notify
const objWithCustomNotify = new Subscribable()
const customNotify = jest.fn((payload) => {
console.log(payload)
return false
})
objWithCustomNotify.subscription = {
notify: customNotify
}
objWithCustomNotify.subscribe(cb)
objWithCustomNotify.notify({ key3: 'val3'})
expect(customNotify).toBeCalledTimes(1)
})
})
9 changes: 4 additions & 5 deletions packages/shared/src/compare.ts
Original file line number Diff line number Diff line change
@@ -44,10 +44,6 @@ function equal(a: any, b: any, filter?: Filter) {
const immutableB = b && b.toJS
if (immutableA !== immutableB) return false
if (immutableA) return a.is ? a.is(b) : a === b
const schemaA = a && a.toJSON
const schemaB = b && b.toJSON
if (schemaA !== schemaB) return false
if (schemaA && schemaB) return equal(a.toJSON(), b.toJSN(), filter)
const dateA = a instanceof Date
const dateB = b instanceof Date
if (dateA !== dateB) {
@@ -56,7 +52,10 @@ function equal(a: any, b: any, filter?: Filter) {
if (dateA && dateB) {
return a.getTime() === b.getTime()
}

const schemaA = a && a.toJSON
const schemaB = b && b.toJSON
if (schemaA !== schemaB) return false
if (schemaA && schemaB) return equal(a.toJSON(), b.toJSON(), filter)
const regexpA = a instanceof RegExp
const regexpB = b instanceof RegExp
if (regexpA !== regexpB) {