ResourceTests.swift 3.01 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
22
		stub( matchApiPath( "items", method: .POST ), builder: buildItemFromRequest( 2 ) )
		stub( matchApiPath( "failedItems", method: .POST ), builder: http( 422 ) )
23
24
25
26
27
28
	}
	
	override func spec() {
		let api = Api( baseURL: baseURL, apiPath: apiPath )
		
		describe( "Resource" ) {
29
			let resource: Resource = api.resource( "items" )
30
			
31
			describe( "item" ) {
32
33
34
				context( "when the request is successful" ) {
					it( "should call the correct endpoint and return the item" ) {
						waitUntil { done in
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
							resource.item( 1 ) { ( item: Item?, error: ApiError? ) in
								expect( item?.id )   == 1
								expect( item?.name ) == "Item 1"
								
								done()
							}
						}
					}
				}
			}
			
			describe( "create" ) {
				context( "when the request is successful" ) {
					let itemToCreate = Item( id: 0, name: "Item 2" )
					it( "should call the correct endpoint and return the created item" ) {
						waitUntil { done in
							resource.create( itemToCreate ) { ( item: Item?, error: ApiError? ) in
								expect( item?.id )   == 2
								expect( item?.name ) == "Item 2"
								
								done()
							}
						}
					}
				}
				
				context( "when there's an error" ) {
62
					let failResource : Resource = api.resource( "failedItems" )
63
					let itemToCreate = Item( id: 0, name: "Item 2" )
64
					it( "should call the correct endpoint and return the correct error" ) {
65
66
67
68
						waitUntil { done in
							failResource.create( itemToCreate ) { ( item: Item?, error: ApiError? ) in
								expect( item ).to( beNil() )
								expect( error ).toNot( beNil() )
69
70
71
72
73
74
75
								
								done()
							}
						}
					}
				}
			}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
			
			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" ) )
				}
			}
			
			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" ) )
				}
			}
			
			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()
						}
					}
				}
			}
107
108
109
		}
	}
}