ResourceTests.swift 4.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
//  ResourceTests.swift
//  Api
//
//  Created by Mauro Bender on 13/8/16.
//  Copyright © 2016 CocoaPods. All rights reserved.
//

import Quick
import Nimble
import Mockingjay
import Api

class ResourceTests: QuickSpec {
	override func setUp() {
		super.setUp()
		
		// Register stubs
		stub( matchApiPath( "items/1" ), builder: json( [ "id": 1, "name": "Item 1" ] ) )
20
		stub( matchApiPath( "items/1/components/1" ), builder: json( [ "id": 1, "name": "Component 1" ] ) )
21
		stub( matchApiPath( "items", method: .POST ), builder: buildItemFromRequest( 2 ) )
22
23
		stub( matchApiPath( "items/1", method: .DELETE ), builder: http( 200 ) )
		stub( matchApiPath( "items/1", method: .PUT ), builder: http( 200 ) )
24
		stub( matchApiPath( "failedItems", method: .POST ), builder: http( 422 ) )
25
		stub( matchApiPath( "items" ), builder: json( [ [ "id": 1, "name": "Item 1" ], [ "id": 2, "name": "Item 2" ] ] ) )
26
27
28
29
30
31
	}
	
	override func spec() {
		let api = Api( baseURL: baseURL, apiPath: apiPath )
		
		describe( "Resource" ) {
32
			let resource: Resource = api.resource( "items" )
33
			
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
			describe( "Requests methods" ) {
				describe( "get" ) {
					it( "should call the correct endpoint" ) {
						waitUntil { done in resource.get( "1" ) { result in done() } }
					}
				}
				
				describe( "post" ) {
					it( "should call the correct endpoint" ) {
						waitUntil { done in resource.post() { result in done() } }
					}
				}
				
				describe( "put" ) {
					it( "should call the correct endpoint" ) {
						waitUntil { done in resource.put( "1" ) { result in done() } }
					}
				}
				
				describe( "delete" ) {
					it( "should call the correct endpoint" ) {
						waitUntil { done in resource.delete( "1" ) { result in done() } }
56
57
58
59
					}
				}
			}
			
60
61
62
63
64
65
66
67
68
			describe( "CRUD methods" ) {
				describe( "item" ) {
					context( "when the request is successful" ) {
						it( "should call the correct endpoint and return the item" ) {
							waitUntil { done in
								resource.item( 1 ) { ( item: Item?, error: ApiError? ) in
									expect( item?.id )   == 1
									expect( item?.name ) == "Item 1"
									
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
									done()
								}
							}
						}
					}
				}
				
				describe( "list" ) {
					context( "when the request is successful" ) {
						it( "should call the correct endpoint and return the item" ) {
							waitUntil { done in
								resource.list() { ( items: [ Item ]?, error: ApiError? ) in
									expect( items?[0].id )   == 1
									expect( items?[0].name ) == "Item 1"
									expect( items?[1].id )   == 2
									expect( items?[1].name ) == "Item 2"
									
86
87
									done()
								}
88
89
90
91
92
							}
						}
					}
				}
				
93
				describe( "list" ) {
94
					context( "when the request is successful" ) {
95
						it( "should call the correct endpoint and return the item" ) {
96
							waitUntil { done in
97
98
99
100
101
								resource.list() { ( items: [ Item ]?, error: ApiError? ) in
									expect( items?[0].id )   == 1
									expect( items?[0].name ) == "Item 1"
									expect( items?[1].id )   == 2
									expect( items?[1].name ) == "Item 2"
102
103
104
105
106
107
									
									done()
								}
							}
						}
					}
108
109
110
111
112
113
114
115
116
117
118
119
				}
				
				describe( "destroy" ) {
					context( "when the request is successful" ) {
						it( "should call the correct endpoint and return the created item" ) {
							waitUntil { done in
								resource.destroy( 1 ) { ( error: ApiError? ) in
									done()
								}
							}
						}
					}
120
121
122
123
124
125
126
127
128
129
130
131
					
					context( "when there's an error" ) {
						let failResource : Resource = api.resource( "failedItems" )
						let itemToCreate = Item( id: 0, name: "Item 2" )
						it( "should call the correct endpoint and return the correct error" ) {
							waitUntil { done in
								failResource.create( itemToCreate ) { ( item: Item?, error: ApiError? ) in
									expect( item ).to( beNil() )
									expect( error ).toNot( beNil() )
									
									done()
								}
132
133
134
135
136
							}
						}
					}
				}
			}
137
			
138
139
140
141
142
143
144
			describe( "nested resources methods" ) {
				describe( "member" ) {
					let member = resource.member( 1 )
					it( "should add the correct path" ) {
						expect( member.api ).to( equal( resource.api ) )
						expect( member.path ).to( equal( "\(resource.path)/1" ) )
					}
145
				}
146
147
148
149
150
151
152
				
				describe( "resource" ) {
					let nestedResource = resource.resource( "components" )
					it( "should add the correct path" ) {
						expect( nestedResource.api ).to( equal( resource.api ) )
						expect( nestedResource.path ).to( equal( "\(resource.path)/components" ) )
					}
153
154
				}
				
155
156
157
158
159
160
161
162
163
164
165
				context( "when getting a nested resource item" ) {
					let nestedResource = resource.member( 1 ).resource( "components" )
					
					it( "should call the correct endpoint and return correct the item" ) {
						waitUntil { done in
							nestedResource.item( 1 ) { ( component: Component?, error: ApiError? ) in
								expect( component?.id )   == 1
								expect( component?.name ) == "Component 1"
								
								done()
							}
166
167
168
169
						}
					}
				}
			}
170
171
172
		}
	}
}