Skip to content

Commit

Permalink
feat:add shared-utils sub package
Browse files Browse the repository at this point in the history
  • Loading branch information
fuqiang15 committed Jul 21, 2021
1 parent bb0c566 commit 1e45ee7
Show file tree
Hide file tree
Showing 13 changed files with 4,139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
},
'overrides': [
{
files: ['packages/dls-color-palette/**/*.spec.js'],
files: ['packages/dls-color-palette/**/*.spec.js', 'packages/shared-utils/**/*.spec.ts'],
env: {
es6: true,
jest: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ npm-debug.log
**/dist
**/variables.*
**/test/snapshots
**/coverage/
2 changes: 2 additions & 0 deletions packages/shared-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# `shared-utils`

64 changes: 64 additions & 0 deletions packages/shared-utils/__tests__/basic/datasource.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { mapDatasource } from '../../src/basic/datasource'

const treeData = [
{
value: 1
},
{
value: 2,
children: [
{
value: 20,
children: [
{
value: 201,
},
{
value: 202,
}
]
},
{
value: 21
}
]
},
{
value: 3
}
]

describe('utils/datasource', () => {
it('should map datasource correctly', () => {
expect(mapDatasource(treeData, item => ({
...item,
value: `${item.value}a`
}))).toEqual([
{
value: '1a'
},
{
value: '2a',
children: [
{
value: '20a',
children: [
{
value: '201a',
},
{
value: '202a',
}
]
},
{
value: '21a'
}
]
},
{
value: '3a'
}
])
})
});
26 changes: 26 additions & 0 deletions packages/shared-utils/__tests__/basic/dom.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { toggleClass } from '../../src/basic/dom'

describe('utils/dom', () => {
it('should toggle classes correctly', () => {
let el = document.createElement('div')
el.className = 'a b c d '
document.body.appendChild(el)

toggleClass(el, 'a')
toggleClass(el, 'b', true)
toggleClass(el, 'c')
toggleClass(el, 'd')
toggleClass(el, 'e')
toggleClass(el, 'f', true)
toggleClass(el, 'g', false)
expect(el.classList.contains('a')).toBe(false)
expect(el.classList.contains('b')).toBe(true)
expect(el.classList.contains('c')).toBe(false)
expect(el.classList.contains('d')).toBe(false)
expect(el.classList.contains('e')).toBe(true)
expect(el.classList.contains('f')).toBe(true)
expect(el.classList.contains('g')).toBe(false)

el.parentNode!.removeChild(el)
})
});
55 changes: 55 additions & 0 deletions packages/shared-utils/__tests__/tree.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getCheckedSubTree } from '../src/tree'


const treeData = [
{
value: 1
},
{
value: 2,
children: [
{
value: 20,
children: [
{
value: 201,
},
{
value: 202,
}
]
},
{
value: 21
}
]
},
{
value: 3
}
]

describe('tree', () => {
it('getCheckedSubtree should extract checked subtree correctly', () => {
expect(getCheckedSubTree(treeData, [1])).toEqual([treeData[0]])
expect(getCheckedSubTree(treeData, [201, 202])).toEqual([{
value: 2,
children: [
treeData[1].children![0]
]
}])
expect(getCheckedSubTree(treeData, [201])).toEqual([{
value: 2,
children: [
{
value: 20,
children: [
{
value: 201,
}
]
}
]
}])
})
});
15 changes: 15 additions & 0 deletions packages/shared-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

module.exports = {
preset: 'ts-jest',
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,

// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
};
Loading

0 comments on commit 1e45ee7

Please sign in to comment.