1+ const util = require ( '../lib/util' ) ;
2+ const model = require ( '../lib/model' ) ;
3+
4+ // Test data
5+ const modelName = 'Model' ;
6+ const modelExample = {
7+ property : { }
8+ } ;
9+
10+ // Start testing
11+ describe ( ' - Model' , ( ) => {
12+ it ( 'shall be an object' , ( ) => {
13+ expect ( typeof model ) . toEqual ( 'object' ) ;
14+ } ) ;
15+
16+ it ( 'shall have a "models" property' , ( ) => {
17+ expect ( model . models ) . toBeDefined ( ) ;
18+ } ) ;
19+
20+ it ( 'shall have a "add" property' , ( ) => {
21+ expect ( model . add ) . toBeDefined ( ) ;
22+ } ) ;
23+
24+ it ( 'shall have a "get" property' , ( ) => {
25+ expect ( model . get ) . toBeDefined ( ) ;
26+ } ) ;
27+
28+ describe ( 'models' , ( ) => {
29+ it ( 'shall be an object' , ( ) => {
30+ expect ( typeof model . models ) . toEqual ( 'object' ) ;
31+ } ) ;
32+
33+ /*it('shall be empty by default', () => {
34+ // This test does not works because of models set in other tests
35+ expect(model.models).toEqual({});
36+ });*/
37+ } ) ;
38+
39+ describe ( 'getter' , ( ) => {
40+ it ( 'shall be a function' , ( ) => {
41+ expect ( typeof model . get ) . toEqual ( 'function' ) ;
42+ } ) ;
43+
44+ describe ( 'by param name' , ( ) => {
45+ it ( 'shall return defined models' , ( ) => {
46+ model . models [ modelName ] = modelExample ;
47+
48+ const result = model . get ( modelName ) ;
49+
50+ expect ( result ) . toEqual ( modelExample ) ;
51+ delete model . models [ modelName ] ;
52+ } ) ;
53+
54+ it ( 'shall return empty object on unsetted model name' , ( ) => {
55+ const result = model . get ( 'undefined' + modelName ) ;
56+ expect ( result ) . toEqual ( { } ) ;
57+ } ) ;
58+
59+ it ( 'shall return empty object on undefined model name' , ( ) => {
60+ const result = model . get ( ) ;
61+ expect ( result ) . toEqual ( { } ) ;
62+ } ) ;
63+ } ) ;
64+
65+ describe ( 'by this.modelName' , ( ) => {
66+ it ( 'shall return defined models' , ( ) => {
67+ // Set model
68+ model . models [ modelName ] = modelExample ;
69+ // Prepare "this" context
70+ const self = {
71+ modelName : modelName ,
72+ get : model . get
73+ }
74+ // Execute getter on "this" context
75+ const result = self . get ( ) ;
76+
77+ expect ( result ) . toEqual ( modelExample ) ;
78+
79+ // Reset models
80+ delete model . models [ modelName ] ;
81+ } ) ;
82+
83+ it ( 'shall return empty object on unsetted model name' , ( ) => {
84+ // Prepare "this" context
85+ const self = {
86+ modelName : 'undefined' + modelName ,
87+ get : model . get
88+ }
89+ // Execute getter on "this" context
90+ const result = self . get ( ) ;
91+
92+ expect ( result ) . toEqual ( { } ) ;
93+ } ) ;
94+
95+ it ( 'shall return empty object on undefined model name' , ( ) => {
96+ // Prepare "this" context
97+ const self = {
98+ get : model . get
99+ }
100+ // Execute getter on "this" context
101+ const result = self . get ( ) ;
102+
103+ expect ( result ) . toEqual ( { } ) ;
104+ } ) ;
105+ } ) ;
106+ } ) ;
107+
108+ describe ( 'adder' , ( ) => {
109+ it ( 'shall be a function' , ( ) => {
110+ expect ( typeof model . add ) . toEqual ( 'function' ) ;
111+ } ) ;
112+
113+ describe ( 'by param name' , ( ) => {
114+ it ( 'shall add models' , ( ) => {
115+ model . add ( modelExample , modelName ) ;
116+ expect ( model . models [ modelName ] ) . toEqual ( modelExample ) ;
117+ delete model . models [ modelName ] ;
118+ } ) ;
119+
120+ it ( 'shall add nothing when no modelName' , ( ) => {
121+ const keys = Object . keys ( model . models ) ;
122+ model . add ( modelExample ) ;
123+ expect ( Object . keys ( model . models ) ) . toEqual ( keys ) ;
124+ } ) ;
125+
126+ it ( 'shall add nothing when no data' , ( ) => {
127+ const keys = Object . keys ( model . models ) ;
128+ model . add ( undefined , modelName ) ;
129+ expect ( Object . keys ( model . models ) ) . toEqual ( keys ) ;
130+ } ) ;
131+
132+ it ( 'shall add nothing when data is not an object' , ( ) => {
133+ const keys = Object . keys ( model . models ) ;
134+ model . add ( 'undefined' , modelName ) ;
135+ expect ( Object . keys ( model . models ) ) . toEqual ( keys ) ;
136+ } ) ;
137+ } ) ;
138+
139+ describe ( 'by this.modelName' , ( ) => {
140+ it ( 'shall add models' , ( ) => {
141+ // Prepare this context
142+ const self = {
143+ modelName : modelName ,
144+ add : model . add
145+ } ;
146+
147+ const keys = Object . keys ( model . models ) ;
148+ self . add ( modelExample ) ;
149+
150+ expect ( Object . keys ( model . models ) . length ) . toEqual ( ( keys . length + 1 ) ) ;
151+ expect ( model . models [ modelName ] ) . toEqual ( modelExample ) ;
152+
153+ // Reset models
154+ delete model . models [ modelName ] ;
155+ } ) ;
156+
157+ it ( 'shall add nothing when no modelName' , ( ) => {
158+ // Prepare this context
159+ const self = {
160+ add : model . add
161+ } ;
162+
163+ const keys = Object . keys ( model . models ) ;
164+ self . add ( modelExample ) ;
165+
166+ expect ( Object . keys ( model . models ) ) . toEqual ( keys ) ;
167+ } ) ;
168+
169+ it ( 'shall add nothing when no data' , ( ) => {
170+ // Prepare this context
171+ const self = {
172+ modelName : modelName ,
173+ add : model . add
174+ } ;
175+
176+ const keys = Object . keys ( model . models ) ;
177+ self . add ( undefined ) ;
178+
179+ expect ( Object . keys ( model . models ) ) . toEqual ( keys ) ;
180+ } ) ;
181+
182+ it ( 'shall add nothing when data is not an object' , ( ) => {
183+ // Prepare this context
184+ const self = {
185+ modelName : modelName ,
186+ add : model . add
187+ } ;
188+
189+ const keys = Object . keys ( model . models ) ;
190+ self . add ( 'undefined' ) ;
191+ expect ( Object . keys ( model . models ) ) . toEqual ( keys ) ;
192+ } ) ;
193+ } ) ;
194+ } ) ;
195+ } ) ;
0 commit comments