@@ -2,6 +2,56 @@ const {expect} = require('../util');
2
2
const { RootModel} = require ( '../../lib' ) ;
3
3
4
4
describe ( 'collections' , ( ) => {
5
+ describe ( 'getOrDefault' , ( ) => {
6
+ it ( 'returns value if defined' , ( ) => {
7
+ const model = new RootModel ( ) ;
8
+ model . add ( '_test_doc' , { name : 'foo' } ) ;
9
+ const value = model . getOrDefault ( '_test_doc' , { name : 'bar' } ) ;
10
+ expect ( value ) . not . to . be . undefined ;
11
+ } ) ;
12
+
13
+ it ( 'returns defuault value if undefined' , ( ) => {
14
+ const model = new RootModel ( ) ;
15
+ const defaultValue = { name : 'bar' } ;
16
+ const value = model . getOrDefault ( '_test_doc' , defaultValue ) ;
17
+ expect ( value ) . not . to . be . undefined ;
18
+ expect ( value . name ) . to . equal ( 'bar' ) ;
19
+ expect ( value ) . to . eql ( defaultValue ) ;
20
+ } ) ;
21
+
22
+ it ( 'returns default value if null' , ( ) => {
23
+ const model = new RootModel ( ) ;
24
+ const id = model . add ( '_test_doc' , { name : null } ) ;
25
+ const defaultValue = 'bar' ;
26
+ const value = model . getOrDefault ( `_test_doc.${ id } .name` , defaultValue ) ;
27
+ expect ( value ) . not . to . be . null ;
28
+ expect ( value ) . to . equal ( 'bar' ) ;
29
+ expect ( value ) . to . eql ( defaultValue ) ;
30
+ } ) ;
31
+ } ) ;
32
+
33
+ describe ( 'getOrThrow' , ( ) => {
34
+ it ( 'returns value if defined' , ( ) => {
35
+ const model = new RootModel ( ) ;
36
+ model . add ( '_test_doc' , { name : 'foo' } ) ;
37
+ const value = model . getOrThrow ( '_test_doc' ) ;
38
+ expect ( value ) . not . to . be . undefined ;
39
+ } ) ;
40
+
41
+ it ( 'throws if value undefined' , ( ) => {
42
+ const model = new RootModel ( ) ;
43
+ expect ( ( ) => model . getOrThrow ( '_test_doc' ) ) . to . throw ( `No value at path _test_doc` ) ;
44
+ expect ( ( ) => model . scope ( '_test' ) . getOrThrow ( 'doc.1' ) ) . to . throw ( `No value at path _test.doc.1` ) ;
45
+ } ) ;
46
+
47
+ it ( 'throws if value null' , ( ) => {
48
+ const model = new RootModel ( ) ;
49
+ const id = model . add ( '_test_doc' , { name : null } ) ;
50
+ expect ( model . getOrThrow ( `_test_doc.${ id } ` ) ) . to . eql ( { id, name : null } ) ;
51
+ expect ( ( ) => model . getOrThrow ( `_test_doc.${ id } .name` ) ) . to . throw ( `No value at path _test_doc` ) ;
52
+ } ) ;
53
+ } ) ;
54
+
5
55
describe ( 'getValues' , ( ) => {
6
56
it ( 'returns array of values from collection' , ( ) => {
7
57
const model = new RootModel ( ) ;
0 commit comments