1
- import { expect } from 'chai'
1
+ import { beforeEach , describe , expect , test } from 'vitest'
2
+ import { makeApi } from '../server'
3
+ let api , mockApi
2
4
3
5
import createStubContext from '../stubs/context'
4
6
import createJsonapiModule from '../utils/createJsonapiModule'
@@ -9,14 +11,15 @@ describe('deleteRelated', function () {
9
11
let normWidget1 , jsonWidget1 , jsonapiModule , stubContext
10
12
11
13
beforeEach ( function ( ) {
14
+ [ api , mockApi ] = makeApi ( )
12
15
normWidget1 = createNormWidget1 ( )
13
16
jsonWidget1 = createJsonWidget1 ( )
14
17
15
- jsonapiModule = createJsonapiModule ( this . api )
18
+ jsonapiModule = createJsonapiModule ( api )
16
19
stubContext = createStubContext ( jsonapiModule )
17
20
} )
18
21
19
- it ( 'Should throw an error if passed an object with no type or id' , async function ( ) {
22
+ test ( 'Should throw an error if passed an object with no type or id' , async function ( ) {
20
23
try {
21
24
await jsonapiModule . actions . deleteRelated ( stubContext , { _jv : { } } )
22
25
throw 'Should have thrown an error (no id)'
@@ -25,7 +28,7 @@ describe('deleteRelated', function () {
25
28
}
26
29
} )
27
30
28
- it ( 'Should throw an error if passed an object with no relationships' , async function ( ) {
31
+ test ( 'Should throw an error if passed an object with no relationships' , async function ( ) {
29
32
try {
30
33
await jsonapiModule . actions . deleteRelated ( stubContext , { _jv : { type : 'widget' , id : 1 } } )
31
34
throw 'Should have thrown an error (no relationships)'
@@ -34,24 +37,24 @@ describe('deleteRelated', function () {
34
37
}
35
38
} )
36
39
37
- it ( 'should make a delete request for the object passed in.' , async function ( ) {
38
- this . mockApi . onDelete ( ) . replyOnce ( 204 )
39
- this . mockApi . onGet ( ) . replyOnce ( 200 , { data : jsonWidget1 } )
40
+ test ( 'should make a delete request for the object passed in.' , async function ( ) {
41
+ mockApi . onDelete ( ) . replyOnce ( 204 )
42
+ mockApi . onGet ( ) . replyOnce ( 200 , { data : jsonWidget1 } )
40
43
41
44
const rel = { data : { type : 'widget' , id : '2' } }
42
45
normWidget1 [ '_jv' ] [ 'relationships' ] = { widgets : rel }
43
46
44
47
await jsonapiModule . actions . deleteRelated ( stubContext , normWidget1 )
45
48
// Expect a delete call to rel url, with rel payload, then get object to update store
46
- expect ( this . mockApi . history . delete [ 0 ] . url ) . to . equal ( 'widget/1/relationships/widgets' )
47
- expect ( this . mockApi . history . delete [ 0 ] . data ) . to . deep . equal ( JSON . stringify ( rel ) )
48
- expect ( this . mockApi . history . get [ 0 ] . params ) . to . have . property ( 'include' )
49
- expect ( this . mockApi . history . get [ 0 ] . url ) . to . equal ( 'widget/1' )
49
+ expect ( mockApi . history . delete [ 0 ] . url ) . to . equal ( 'widget/1/relationships/widgets' )
50
+ expect ( mockApi . history . delete [ 0 ] . data ) . to . deep . equal ( JSON . stringify ( rel ) )
51
+ expect ( mockApi . history . get [ 0 ] . params ) . to . have . property ( 'include' )
52
+ expect ( mockApi . history . get [ 0 ] . url ) . to . equal ( 'widget/1' )
50
53
} )
51
54
52
- it ( 'should make a delete request for the object passed in.' , async function ( ) {
53
- this . mockApi . onDelete ( ) . replyOnce ( 204 )
54
- this . mockApi . onGet ( ) . replyOnce ( 200 , { data : jsonWidget1 } )
55
+ test ( 'should make a delete request for the object passed in.' , async function ( ) {
56
+ mockApi . onDelete ( ) . replyOnce ( 204 )
57
+ mockApi . onGet ( ) . replyOnce ( 200 , { data : jsonWidget1 } )
55
58
56
59
const rel = { data : { type : 'widget' , id : '2' } }
57
60
normWidget1 [ '_jv' ] [ 'relationships' ] = { widgets : rel }
@@ -60,33 +63,33 @@ describe('deleteRelated', function () {
60
63
61
64
await jsonapiModule . actions . deleteRelated ( stubContext , normWidget1 )
62
65
// Expect a delete call to rel url, with rel payload, then get object to update store
63
- expect ( this . mockApi . history . delete [ 0 ] . url ) . to . equal ( 'widget/1/relationships/widgets' )
64
- expect ( this . mockApi . history . delete [ 0 ] . data ) . to . deep . equal ( JSON . stringify ( rel ) )
65
- expect ( this . mockApi . history . get [ 0 ] . params ) . to . not . have . property ( 'include' )
66
- expect ( this . mockApi . history . get [ 0 ] . url ) . to . equal ( 'widget/1' )
66
+ expect ( mockApi . history . delete [ 0 ] . url ) . to . equal ( 'widget/1/relationships/widgets' )
67
+ expect ( mockApi . history . delete [ 0 ] . data ) . to . deep . equal ( JSON . stringify ( rel ) )
68
+ expect ( mockApi . history . get [ 0 ] . params ) . to . not . have . property ( 'include' )
69
+ expect ( mockApi . history . get [ 0 ] . url ) . to . equal ( 'widget/1' )
67
70
} )
68
71
69
- it ( 'should handle multiple relationships' , async function ( ) {
70
- this . mockApi . onDelete ( ) . reply ( 204 )
71
- this . mockApi . onGet ( ) . replyOnce ( 200 , { data : jsonWidget1 } )
72
+ test ( 'should handle multiple relationships' , async function ( ) {
73
+ mockApi . onDelete ( ) . reply ( 204 )
74
+ mockApi . onGet ( ) . replyOnce ( 200 , { data : jsonWidget1 } )
72
75
73
76
const rel1 = { data : { type : 'widget' , id : '2' } }
74
77
const rel2 = { data : { type : 'doohickey' , id : '3' } }
75
78
normWidget1 [ '_jv' ] [ 'relationships' ] = { widgets : rel1 , doohickeys : rel2 }
76
79
77
80
await jsonapiModule . actions . deleteRelated ( stubContext , normWidget1 )
78
- expect ( this . mockApi . history . delete [ 0 ] . url ) . to . equal ( 'widget/1/relationships/widgets' )
79
- expect ( this . mockApi . history . delete [ 0 ] . data ) . to . deep . equal ( JSON . stringify ( rel1 ) )
80
- expect ( this . mockApi . history . delete [ 1 ] . url ) . to . equal ( 'widget/1/relationships/doohickeys' )
81
- expect ( this . mockApi . history . delete [ 1 ] . data ) . to . deep . equal ( JSON . stringify ( rel2 ) )
82
- expect ( this . mockApi . history . get [ 0 ] . params ) . to . have . property ( 'include' )
83
- expect ( this . mockApi . history . get [ 0 ] . url ) . to . equal ( 'widget/1' )
81
+ expect ( mockApi . history . delete [ 0 ] . url ) . to . equal ( 'widget/1/relationships/widgets' )
82
+ expect ( mockApi . history . delete [ 0 ] . data ) . to . deep . equal ( JSON . stringify ( rel1 ) )
83
+ expect ( mockApi . history . delete [ 1 ] . url ) . to . equal ( 'widget/1/relationships/doohickeys' )
84
+ expect ( mockApi . history . delete [ 1 ] . data ) . to . deep . equal ( JSON . stringify ( rel2 ) )
85
+ expect ( mockApi . history . get [ 0 ] . params ) . to . have . property ( 'include' )
86
+ expect ( mockApi . history . get [ 0 ] . url ) . to . equal ( 'widget/1' )
84
87
// Only get the object once at end
85
- expect ( this . mockApi . history . get . length ) . to . equal ( 1 )
88
+ expect ( mockApi . history . get . length ) . to . equal ( 1 )
86
89
} )
87
90
88
- it ( 'Should handle API errors (in the data)' , async function ( ) {
89
- this . mockApi . onDelete ( ) . reply ( 500 )
91
+ test ( 'Should handle API errors (in the data)' , async function ( ) {
92
+ mockApi . onDelete ( ) . reply ( 500 )
90
93
91
94
const rel = { data : { type : 'widget' , id : '2' } }
92
95
normWidget1 [ '_jv' ] [ 'relationships' ] = { widgets : rel }
0 commit comments