Skip to content

Commit

Permalink
test(EnumData): Supplementary unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwong committed Nov 8, 2021
1 parent d0d9301 commit 5741ffc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@babel/runtime": "^7.16.0"
},
"lint-staged": {
"*.{.ts,.js}": [
"*.{ts,js}": [
"eslint --fix --format=pretty"
]
}
Expand Down
8 changes: 3 additions & 5 deletions src/EnumData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type EnumDataRecord = Record<PropertyKey, any>

type TransformArrayToObject<
Tuple extends EnumDataList,
Result extends EnumDataRecord = {}
Result extends EnumDataRecord = {},
> = Tuple extends []
? Result // last call
: Tuple extends readonly [infer Head, ...infer Tail]
Expand Down Expand Up @@ -87,10 +87,8 @@ export function EnumData<T extends EnumDataList>(data: T) {
return undefined
},
set() {
// eslint-disable-next-line no-console
console.warn('Don’t allow assignment to constant variable')
return false
throw TypeError('Don’t allow assignment to constant variable')
},
})
return (ans as unknown) as EnumDataResult<T>
return ans as unknown as EnumDataResult<T>
}
34 changes: 32 additions & 2 deletions test/EnumData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { EnumData } from '../src'

describe('EnumData:', () => {
/**
* EnumData bace function
* EnumData base function
*/
describe('EnumData bace function', () => {
describe('EnumData base function', () => {
const COLOR_DATA = EnumData([
['RED', 1, '红色'],
['BLUE', 2, '蓝色'],
Expand Down Expand Up @@ -60,6 +60,17 @@ describe('EnumData:', () => {
STATUS_MAP.map.constructor,
Array.prototype.map.constructor,
)

assert.strictEqual(STATUS_MAP.length, 3)

assert.strictEqual(
STATUS_MAP.map(([, , text]) => text).join(','),
'待支付,待回款,待审核',
)
assert.strictEqual(
STATUS_MAP.reduce((prevText, [key]) => `${prevText}${key},`, ''),
'PAY,BALANCE,REVIEW,',
)
})

test(' match in EnumData', () => {
Expand Down Expand Up @@ -115,4 +126,23 @@ describe('EnumData:', () => {
assert.strictEqual(STATUS_MAP.PAY === 10, true)
})
})

/**
* EnumData Set
*/
describe('EnumData Set', () => {
// 字段冲突、方法名冲突
const STATUS_MAP = EnumData([
['YES', 1, '是'],
['NO', 0, '否'],
])

test(' cannot set value ', () => {
assert.strictEqual(STATUS_MAP.YES === 1, true)
expect(() => {
STATUS_MAP.YES = '2'
}).toThrowError('Don’t allow assignment to constant variable')
assert.strictEqual(STATUS_MAP.YES === 1, true)
})
})
})

0 comments on commit 5741ffc

Please sign in to comment.