@@ -25,6 +25,8 @@ class Post extends BaseAutoIncrementModel {
25
25
26
26
@field ( )
27
27
public content : string ;
28
+
29
+ public notAField ?: string ;
28
30
}
29
31
30
32
class Empty extends BaseAutoIncrementModel { }
@@ -46,6 +48,28 @@ function nextTitle() {
46
48
return `post title ${ ++ postTitleCounter } ` ;
47
49
}
48
50
51
+ describe ( '$hasField' , ( ) => {
52
+ it ( 'should return true if field exists' , ( ) => {
53
+ expect ( Post . $hasField ( 'title' ) ) . toBe ( true ) ;
54
+ } ) ;
55
+
56
+ it ( 'should return false if field does not exist' , ( ) => {
57
+ expect ( Post . $hasField ( 'notAField' ) ) . toBe ( false ) ;
58
+ } ) ;
59
+ } ) ;
60
+
61
+ describe ( '$getField' , ( ) => {
62
+ it ( 'should return the field if it exists' , ( ) => {
63
+ const field = Post . $getField ( 'title' ) ;
64
+ expect ( field ) . toStrictEqual ( { } ) ;
65
+ } ) ;
66
+
67
+ it ( 'should return undefined if the field does not exist' , ( ) => {
68
+ const field = Post . $getField ( 'notAField' ) ;
69
+ expect ( field ) . toBeUndefined ( ) ;
70
+ } ) ;
71
+ } ) ;
72
+
49
73
test ( 'can create' , async ( ) => {
50
74
const newUser = await User . create ( {
51
75
username : nextUsername ( ) ,
@@ -59,11 +83,6 @@ test('get collection by model class', async () => {
59
83
expect ( collection ) . toBeDefined ( ) ;
60
84
} ) ;
61
85
62
- test ( 'find one by property' , async ( ) => {
63
- const user = User . findBy ( 'username' , 'root' ) ;
64
- expect ( user ) . toBeDefined ( ) ;
65
- } ) ;
66
-
67
86
test ( 'count all' , async ( ) => {
68
87
const count = await User . count ( { } ) ;
69
88
expect ( count ) . toBe ( 1 ) ;
@@ -78,13 +97,6 @@ test('find by id should work', async () => {
78
97
expect ( secondUser ) . not . toBeNull ( ) ;
79
98
} ) ;
80
99
81
- test ( "find by id should throw when doesn't exists" , async ( ) => {
82
- const t = async ( ) => {
83
- await User . findOrFail ( new ObjectId ( ) ) ;
84
- } ;
85
- await expect ( t ) . rejects . toThrow ( / E _ D O C U M E N T _ N O T _ F O U N D / ) ;
86
- } ) ;
87
-
88
100
test ( 'find all' , async ( ) => {
89
101
const users = await User . all ( ) ;
90
102
expect ( users ) . toHaveLength ( 2 ) ;
@@ -440,3 +452,78 @@ test('custom inspect function', async () => {
440
452
const inspected = inspect ( post ) ;
441
453
expect ( inspected ) . toMatchSnapshot ( ) ;
442
454
} ) ;
455
+
456
+ describe ( 'findMany' , ( ) => {
457
+ it ( 'should accept an empty list' , async ( ) => {
458
+ expect ( await Post . findMany ( [ ] ) ) . toStrictEqual ( [ ] ) ;
459
+ } ) ;
460
+
461
+ it ( 'should find all results' , async ( ) => {
462
+ const results = await Post . findMany ( [ 2 , 1 , 3 ] ) ;
463
+ expect ( results ) . toHaveLength ( 3 ) ;
464
+ expect ( results [ 0 ] ) . toBeInstanceOf ( Post ) ;
465
+ expect ( results . map ( ( value ) => value . id ) ) . toStrictEqual ( [ 1 , 2 , 3 ] ) ;
466
+ } ) ;
467
+
468
+ it ( 'should not duplicate results' , async ( ) => {
469
+ const results = await Post . findMany ( [ 1 , 1 , 1 ] ) ;
470
+ expect ( results ) . toHaveLength ( 1 ) ;
471
+ expect ( results [ 0 ] . id ) . toBe ( 1 ) ;
472
+ } ) ;
473
+ } ) ;
474
+
475
+ describe ( 'findOrFail' , ( ) => {
476
+ it ( 'should return instance if found' , async ( ) => {
477
+ const post = await Post . findOrFail ( 1 ) ;
478
+ expect ( post ) . toBeInstanceOf ( Post ) ;
479
+ expect ( post . id ) . toBe ( 1 ) ;
480
+ } ) ;
481
+
482
+ it ( 'should throw if not found' , async ( ) => {
483
+ await expect ( Post . findOrFail ( - 1 ) ) . rejects . toThrow ( / E _ D O C U M E N T _ N O T _ F O U N D / ) ;
484
+ } ) ;
485
+ } ) ;
486
+
487
+ describe ( 'findBy' , ( ) => {
488
+ it ( 'should return instance if found' , async ( ) => {
489
+ const user = await User . findBy ( 'username' , 'root1' ) ;
490
+ expect ( user ) . toBeInstanceOf ( User ) ;
491
+ // @ts -expect-error
492
+ expect ( user . username ) . toBe ( 'root1' ) ;
493
+ } ) ;
494
+
495
+ it ( 'should return null if not found' , async ( ) => {
496
+ const user = await User . findBy ( 'username' , 'bad' ) ;
497
+ expect ( user ) . toBeNull ( ) ;
498
+ } ) ;
499
+ } ) ;
500
+
501
+ describe ( 'findByOrFail' , ( ) => {
502
+ it ( 'should return instance if found' , async ( ) => {
503
+ const user = await User . findByOrFail ( 'username' , 'root1' ) ;
504
+ expect ( user ) . toBeInstanceOf ( User ) ;
505
+ expect ( user . username ) . toBe ( 'root1' ) ;
506
+ } ) ;
507
+
508
+ it ( 'should throw if not found' , async ( ) => {
509
+ await expect ( User . findByOrFail ( 'username' , 'bad' ) ) . rejects . toThrow (
510
+ / E _ D O C U M E N T _ N O T _ F O U N D / ,
511
+ ) ;
512
+ } ) ;
513
+ } ) ;
514
+
515
+ describe ( 'save' , ( ) => {
516
+ it ( 'should return true if something was saved' , async ( ) => {
517
+ const post = await Post . findOrFail ( 1 ) ;
518
+ post . title = 'new title' ;
519
+ expect ( await post . save ( ) ) . toBe ( true ) ;
520
+ } ) ;
521
+
522
+ it ( 'should return false if nothing was saved' , async ( ) => {
523
+ const post = await Post . findOrFail ( 1 ) ;
524
+ const title = post . title ;
525
+ // no-op
526
+ post . title = title ;
527
+ expect ( await post . save ( ) ) . toBe ( false ) ;
528
+ } ) ;
529
+ } ) ;
0 commit comments