11import Enum from './index'
22
3- test ( 'creates enum and assigns strings' , ( ) => {
4- const { Summer, Autumn, Winter, Spring } = Enum . String ( )
3+ describe ( 'String Enums' , ( ) => {
4+ test ( 'creates enum and assigns strings' , ( ) => {
5+ const { Summer, Autumn, Winter, Spring } = Enum . String ( )
6+
7+ expect ( Summer ) . toEqual ( 'Summer' )
8+ expect ( Autumn ) . toEqual ( 'Autumn' )
9+ expect ( Winter ) . toEqual ( 'Winter' )
10+ expect ( Spring ) . toEqual ( 'Spring' )
11+ } )
12+
13+ test ( 'creates enum with snakeCase casing' , ( ) => {
14+ const { userId, userAddress, orderNumber } = Enum . String ( { casing : 'snakeCase' } )
15+
16+ expect ( userId ) . toEqual ( 'user_id' )
17+ expect ( userAddress ) . toEqual ( 'user_address' )
18+ expect ( orderNumber ) . toEqual ( 'order_number' )
19+ } )
20+
21+ test ( 'creates enum with camelCase casing' , ( ) => {
22+ const { User_Id, User_Address, Order_Number } = Enum . String ( { casing : 'camelCase' } )
23+
24+ expect ( User_Id ) . toEqual ( 'userId' )
25+ expect ( User_Address ) . toEqual ( 'userAddress' )
26+ expect ( Order_Number ) . toEqual ( 'orderNumber' )
27+ } )
28+
29+ test ( 'creates enum with PascalCase casing' , ( ) => {
30+ const { user_id, user_address, order_number } = Enum . String ( { casing : 'PascalCase' } )
31+
32+ expect ( user_id ) . toEqual ( 'UserId' )
33+ expect ( user_address ) . toEqual ( 'UserAddress' )
34+ expect ( order_number ) . toEqual ( 'OrderNumber' )
35+ } )
36+
37+ test ( 'creates enum with kebabCase casing' , ( ) => {
38+ const { UserId, UserAddress, OrderNumber } = Enum . String ( { casing : 'kebabCase' } )
39+
40+ expect ( UserId ) . toEqual ( 'user-id' )
41+ expect ( UserAddress ) . toEqual ( 'user-address' )
42+ expect ( OrderNumber ) . toEqual ( 'order-number' )
43+ } )
44+
45+ test ( 'creates enum with prefix' , ( ) => {
46+ const { Summer, Winter } = Enum . String ( { prefix : 'Season_' } ) ;
47+
48+ expect ( Summer ) . toEqual ( 'Season_Summer' ) ;
49+ expect ( Winter ) . toEqual ( 'Season_Winter' ) ;
50+ } ) ;
51+
52+ test ( 'creates enum with suffix' , ( ) => {
53+ const { Summer, Winter } = Enum . String ( { suffix : '_Season' } ) ;
54+
55+ expect ( Summer ) . toEqual ( 'Summer_Season' ) ;
56+ expect ( Winter ) . toEqual ( 'Winter_Season' ) ;
57+ } ) ;
58+
59+ test ( 'creates enum with both prefix and suffix' , ( ) => {
60+ const { Summer, Winter } = Enum . String ( { prefix : 'Season_' , suffix : '_Time' } ) ;
61+
62+ expect ( Summer ) . toEqual ( 'Season_Summer_Time' ) ;
63+ expect ( Winter ) . toEqual ( 'Season_Winter_Time' ) ;
64+ } ) ;
65+
66+ test ( 'creates enum with transform function' , ( ) => {
67+ const transformFn = ( value ) => `Transformed_${ value } ` ;
68+ const { Summer, Winter } = Enum . String ( { transform : transformFn } ) ;
69+
70+ expect ( Summer ) . toEqual ( 'Transformed_Summer' ) ;
71+ expect ( Winter ) . toEqual ( 'Transformed_Winter' ) ;
72+ } ) ;
73+
74+ test ( 'creates enum with transform function and casing' , ( ) => {
75+ const transformFn = ( value ) => `Transformed_${ value } ` ;
76+ const { summer_time, winter_time } = Enum . String ( { transform : transformFn , casing : 'PascalCase' } ) ;
77+
78+ expect ( summer_time ) . toEqual ( 'Transformed_SummerTime' ) ;
79+ expect ( winter_time ) . toEqual ( 'Transformed_WinterTime' ) ;
80+ } ) ;
81+ } ) ;
582
6- expect ( Summer ) . toEqual ( 'Summer' )
7- expect ( Autumn ) . toEqual ( 'Autumn' )
8- expect ( Winter ) . toEqual ( 'Winter' )
9- expect ( Spring ) . toEqual ( 'Spring' )
83+ describe ( 'Numeric Enums' , ( ) => {
84+ test ( 'creates enum and assigns numeric value' , ( ) => {
85+ const { A, B, C, D } = Enum . Numeric ( )
86+
87+ expect ( A ) . toBe ( 0 )
88+ expect ( B ) . toBe ( 1 )
89+ expect ( C ) . toBe ( 2 )
90+ expect ( D ) . toBe ( 3 )
91+ } )
92+
93+ test ( 'creates enum and assigns numeric value starting at index of choice' , ( ) => {
94+ const { A, B, C, D } = Enum . Numeric ( { startIndex : 1 } )
95+
96+ expect ( A ) . toBe ( 1 )
97+ expect ( B ) . toBe ( 2 )
98+ expect ( C ) . toBe ( 3 )
99+ expect ( D ) . toBe ( 4 )
100+ } )
101+
102+ test ( 'creates enum and assigns numeric value with a specific step' , ( ) => {
103+ const { A, B, C, D } = Enum . Numeric ( { startIndex : 0 , step : 5 } ) ;
104+
105+ expect ( A ) . toBe ( 0 ) ;
106+ expect ( B ) . toBe ( 5 ) ;
107+ expect ( C ) . toBe ( 10 ) ;
108+ expect ( D ) . toBe ( 15 ) ;
109+ } ) ;
110+
111+ test ( 'ensures numeric enums are stateless and start from the first accessed key' , ( ) => {
112+ const { B, A, C } = Enum . Numeric ( )
113+ const { D, E } = Enum . Numeric ( )
114+ const { F, G } = Enum . Numeric ( { startIndex : 5 } )
115+ const { H, I } = Enum . Numeric ( )
116+
117+ expect ( B ) . toBe ( 0 )
118+ expect ( A ) . toBe ( 1 )
119+ expect ( C ) . toBe ( 2 )
120+ expect ( D ) . toBe ( 0 )
121+ expect ( E ) . toBe ( 1 )
122+ expect ( F ) . toBe ( 5 )
123+ expect ( G ) . toBe ( 6 )
124+ expect ( H ) . toBe ( 0 )
125+ expect ( I ) . toBe ( 1 )
126+ } )
10127} )
11128
12- test ( 'creates enum and assigns numeric value' , ( ) => {
13- const { A, B, C, D } = Enum . Numeric ( )
14-
15- expect ( A ) . toBe ( 0 )
16- expect ( B ) . toBe ( 1 )
17- expect ( C ) . toBe ( 2 )
18- expect ( D ) . toBe ( 3 )
129+ describe ( 'Symbol Enums' , ( ) => {
130+ test ( 'creates enum and assigns symbol values' , ( ) => {
131+ const { blue, red } = Enum . Symbol ( )
132+ const { blue : blueMood , happy } = Enum . Symbol ( )
133+
134+ expect ( blue ) . toBe ( blue )
135+ expect ( blue ) . not . toBe ( red )
136+ expect ( blue ) . not . toBe ( blueMood )
137+ expect ( blue ) . not . toBe ( 'blue' )
138+ expect ( blue ) . not . toBe ( Symbol ( 'blue' ) )
139+ } )
140+
141+ test ( 'creates global symbol values' , ( ) => {
142+ const { globalBlue, globalRed } = Enum . Symbol ( { global : true } ) ;
143+ const { globalBlue : anotherGlobalBlue } = Enum . Symbol ( { global : true } ) ;
144+
145+ expect ( globalBlue ) . toBe ( globalBlue ) ;
146+ expect ( globalBlue ) . toBe ( anotherGlobalBlue ) ; // Both should reference the same global symbol
147+ expect ( globalBlue ) . not . toBe ( globalRed ) ;
148+ expect ( globalBlue ) . not . toBe ( 'globalBlue' ) ;
149+ expect ( globalBlue ) . toBe ( Symbol . for ( 'globalBlue' ) ) ; // Should match the global symbol
150+ } )
19151} )
20-
21- test ( 'creates enum and assigns numeric value starting at index of choice' , ( ) => {
22- const { A, B, C, D } = Enum . Numeric ( 1 )
23-
24- expect ( A ) . toBe ( 1 )
25- expect ( B ) . toBe ( 2 )
26- expect ( C ) . toBe ( 3 )
27- expect ( D ) . toBe ( 4 )
28- } )
29-
30- test ( 'ensures numeric enums are stateless and start from the first accessed key' , ( ) => {
31- const { B, A, C } = Enum . Numeric ( )
32- const { D, E } = Enum . Numeric ( )
33- const { F, G } = Enum . Numeric ( 5 )
34- const { H, I } = Enum . Numeric ( )
35-
36- expect ( B ) . toBe ( 0 )
37- expect ( A ) . toBe ( 1 )
38- expect ( C ) . toBe ( 2 )
39- expect ( D ) . toBe ( 0 )
40- expect ( E ) . toBe ( 1 )
41- expect ( F ) . toBe ( 5 )
42- expect ( G ) . toBe ( 6 )
43- expect ( H ) . toBe ( 0 )
44- expect ( I ) . toBe ( 1 )
45- } )
46-
47- test ( 'creates enum and assigns symbol values' , ( ) => {
48- const { blue, red } = Enum . Symbol ( )
49- const { blue : blueMood , happy } = Enum . Symbol ( )
50-
51- expect ( blue ) . toBe ( blue )
52- expect ( blue ) . not . toBe ( red )
53- expect ( blue ) . not . toBe ( blueMood )
54- expect ( blue ) . not . toBe ( 'blue' )
55- expect ( blue ) . not . toBe ( Symbol ( 'blue' ) )
56- } )
0 commit comments