1+ import { addClass , createQueueKey , hasClass , isEmpty , removeByIndex , removeClass } from './utils' ;
2+
3+ describe ( 'removeByIndex' , ( ) => {
4+ it ( '應正確移除指定索引的元素' , ( ) => {
5+ expect ( removeByIndex ( [ 1 , 2 , 3 ] , 1 ) ) . toEqual ( [ 1 , 3 ] ) ;
6+ } ) ;
7+ it ( '索引為 -1 或超出範圍時應回傳原陣列' , ( ) => {
8+ expect ( removeByIndex ( [ 1 , 2 , 3 ] , - 1 ) ) . toEqual ( [ 1 , 2 , 3 ] ) ;
9+ expect ( removeByIndex ( [ 1 , 2 , 3 ] , 3 ) ) . toEqual ( [ 1 , 2 , 3 ] ) ;
10+ } ) ;
11+ } ) ;
12+
13+ describe ( 'hasClass' , ( ) => {
14+ it ( '應正確判斷 class 是否存在' , ( ) => {
15+ const dom = { className : 'foo bar' } ;
16+ expect ( hasClass ( dom , 'foo' ) ) . toBe ( true ) ;
17+ expect ( hasClass ( dom , 'bar' ) ) . toBe ( true ) ;
18+ expect ( hasClass ( dom , 'baz' ) ) . toBe ( false ) ;
19+ } ) ;
20+ } ) ;
21+
22+ describe ( 'addClass' , ( ) => {
23+ it ( '應新增 class 到 dom' , ( ) => {
24+ const dom = { className : '' , classList : { add : jest . fn ( ) } } ;
25+ addClass ( dom , 'foo' ) ;
26+ expect ( dom . classList . add ) . toHaveBeenCalledWith ( 'foo' ) ;
27+ } ) ;
28+ it ( '已存在 class 不應重複新增' , ( ) => {
29+ const dom = { className : 'foo' , classList : { add : jest . fn ( ) } } ;
30+ addClass ( dom , 'foo' ) ;
31+ expect ( dom . classList . add ) . not . toHaveBeenCalled ( ) ;
32+ } ) ;
33+ } ) ;
34+
35+ describe ( 'removeClass' , ( ) => {
36+ it ( '應從 dom 移除 class' , ( ) => {
37+ const dom = { classList : { remove : jest . fn ( ) } } ;
38+ removeClass ( dom , 'foo' ) ;
39+ expect ( dom . classList . remove ) . toHaveBeenCalledWith ( 'foo' ) ;
40+ } ) ;
41+ } ) ;
42+
43+ describe ( 'createQueueKey' , ( ) => {
44+ it ( '應產生唯一且為小寫的 queueKey' , ( ) => {
45+ const key1 = createQueueKey ( ) ;
46+ const key2 = createQueueKey ( ) ;
47+ expect ( typeof key1 ) . toBe ( 'string' ) ;
48+ expect ( key1 ) . toMatch ( / ^ [ 0 - 9 a - h ] { 26 } $ / ) ; // ulid 格式
49+ expect ( key1 ) . toBe ( key1 . toLowerCase ( ) ) ;
50+ expect ( key1 ) . not . toBe ( key2 ) ;
51+ } ) ;
52+ } ) ;
53+
54+ describe ( 'isEmpty' , ( ) => {
55+ it ( '應判斷 undefined, null, false, 0, 空字串, 空物件為空' , ( ) => {
56+ expect ( isEmpty ( undefined ) ) . toBe ( true ) ;
57+ expect ( isEmpty ( null ) ) . toBe ( true ) ;
58+ expect ( isEmpty ( false ) ) . toBe ( true ) ;
59+ expect ( isEmpty ( 0 ) ) . toBe ( true ) ;
60+ expect ( isEmpty ( '' ) ) . toBe ( true ) ;
61+ expect ( isEmpty ( { } ) ) . toBe ( true ) ;
62+ } ) ;
63+ it ( '應判斷非空值' , ( ) => {
64+ expect ( isEmpty ( 1 ) ) . toBe ( false ) ;
65+ expect ( isEmpty ( 'abc' ) ) . toBe ( false ) ;
66+ expect ( isEmpty ( [ 1 ] ) ) . toBe ( false ) ;
67+ expect ( isEmpty ( { a : 1 } ) ) . toBe ( false ) ;
68+ expect ( isEmpty ( new Date ( ) ) ) . toBe ( false ) ;
69+ } ) ;
70+ it ( '可自訂 isZero, isFalse 選項' , ( ) => {
71+ expect ( isEmpty ( 0 , { isZero : false } ) ) . toBe ( false ) ;
72+ expect ( isEmpty ( false , { isFalse : false } ) ) . toBe ( false ) ;
73+ } ) ;
74+ } ) ;
0 commit comments