|
| 1 | +import XCTest |
| 2 | +@testable import WordPressKit |
| 3 | + |
| 4 | +class EditorServiceRemoteTests: XCTestCase { |
| 5 | + |
| 6 | + let mockRemoteApi = MockWordPressComRestApi() |
| 7 | + var editorServiceRemote: EditorServiceRemote! |
| 8 | + let siteID = 99999 |
| 9 | + |
| 10 | + override func setUp() { |
| 11 | + super.setUp() |
| 12 | + editorServiceRemote = EditorServiceRemote(wordPressComRestApi: mockRemoteApi) |
| 13 | + } |
| 14 | + |
| 15 | + // MARK: - POST tests |
| 16 | + |
| 17 | + func testPostDesignateMobileEditorPostMethodIsCalled() { |
| 18 | + editorServiceRemote.postDesignateMobileEditor(siteID, editor: .gutenberg, success: { _ in }, failure: { _ in }) |
| 19 | + XCTAssertTrue(mockRemoteApi.postMethodCalled) |
| 20 | + } |
| 21 | + |
| 22 | + func testPostDesignateMobileEditorSuccessSettingGutenberg() { |
| 23 | + let expec = expectation(description: "success") |
| 24 | + let response = mockResponse(forMobile: .gutenberg, andWeb: .gutenberg) |
| 25 | + |
| 26 | + editorServiceRemote.postDesignateMobileEditor(siteID, editor: .gutenberg, success: { editor in |
| 27 | + XCTAssertEqual(editor.mobile, .gutenberg) |
| 28 | + XCTAssertEqual(editor.web, .gutenberg) |
| 29 | + expec.fulfill() |
| 30 | + }) { (error) in |
| 31 | + XCTFail("This call should succeed. Error: \(error)") |
| 32 | + expec.fulfill() |
| 33 | + } |
| 34 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 35 | + |
| 36 | + wait(for: [expec], timeout: 0.1) |
| 37 | + } |
| 38 | + |
| 39 | + func testPostDesignateMobileEditorSuccessSettingAztec() { |
| 40 | + let expec = expectation(description: "success") |
| 41 | + let response = mockResponse(forMobile: .aztec, andWeb: .classic) |
| 42 | + |
| 43 | + editorServiceRemote.postDesignateMobileEditor(siteID, editor: .aztec, success: { editor in |
| 44 | + XCTAssertEqual(editor.mobile, .aztec) |
| 45 | + XCTAssertEqual(editor.web, .classic) |
| 46 | + expec.fulfill() |
| 47 | + }) { (error) in |
| 48 | + XCTFail("This call should succeed. Error: \(error)") |
| 49 | + expec.fulfill() |
| 50 | + } |
| 51 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 52 | + |
| 53 | + wait(for: [expec], timeout: 0.1) |
| 54 | + } |
| 55 | + |
| 56 | + func testPostDesignateMobileEditorDoesNotCrashWithBadKeyResponse() { |
| 57 | + let expec = expectation(description: "success") |
| 58 | + let response: [String: String] = [ |
| 59 | + "editor_mobile_bad": "gutenberg", |
| 60 | + "editor_web": "gutenberg", |
| 61 | + ] |
| 62 | + |
| 63 | + editorServiceRemote.postDesignateMobileEditor(siteID, editor: .gutenberg, success: { editor in |
| 64 | + XCTFail("This should fail") |
| 65 | + expec.fulfill() |
| 66 | + }) { (error) in |
| 67 | + let nsError = error as NSError |
| 68 | + XCTAssertEqual(nsError.code, NSCoderValueNotFoundError) |
| 69 | + expec.fulfill() |
| 70 | + } |
| 71 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 72 | + |
| 73 | + wait(for: [expec], timeout: 0.1) |
| 74 | + } |
| 75 | + |
| 76 | + func testPostDesignateMobileEditorThrowsErrorWithBadValueResponse() { |
| 77 | + let expec = expectation(description: "success") |
| 78 | + let response: [String: String] = [ |
| 79 | + "editor_mobile": "guten_BORG", |
| 80 | + "editor_web": "guten_WRONG", |
| 81 | + ] |
| 82 | + editorServiceRemote.postDesignateMobileEditor(siteID, editor: .gutenberg, success: { editor in |
| 83 | + XCTFail("This should throw an error") |
| 84 | + expec.fulfill() |
| 85 | + }) { (error) in |
| 86 | + XCTAssertEqual(error as NSError, EditorSettings.Error.decodingFailed as NSError) |
| 87 | + expec.fulfill() |
| 88 | + } |
| 89 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 90 | + |
| 91 | + wait(for: [expec], timeout: 0.1) |
| 92 | + } |
| 93 | + |
| 94 | + func testPostDesignateMobileEditorError() { |
| 95 | + let expec = expectation(description: "success") |
| 96 | + let errorExpec = NSError(domain: NSURLErrorDomain, code: NSURLErrorUnknown, userInfo: nil) |
| 97 | + editorServiceRemote.postDesignateMobileEditor(siteID, editor: .gutenberg, success: { _ in |
| 98 | + XCTFail("This call should error") |
| 99 | + expec.fulfill() |
| 100 | + }) { (error) in |
| 101 | + XCTAssertEqual(error as NSError, errorExpec) |
| 102 | + expec.fulfill() |
| 103 | + } |
| 104 | + mockRemoteApi.failureBlockPassedIn?(errorExpec, nil) |
| 105 | + XCTAssertTrue(mockRemoteApi.postMethodCalled) |
| 106 | + |
| 107 | + wait(for: [expec], timeout: 0.1) |
| 108 | + } |
| 109 | + |
| 110 | + // MARK: - GET tests |
| 111 | + |
| 112 | + func testGetEditorSettingsGutenberg() { |
| 113 | + let expec = expectation(description: "success") |
| 114 | + let response = mockResponse(forMobile: .gutenberg, andWeb: .gutenberg) |
| 115 | + |
| 116 | + editorServiceRemote.getEditorSettings(siteID, success: { (editor) in |
| 117 | + XCTAssertEqual(editor.mobile, .gutenberg) |
| 118 | + XCTAssertEqual(editor.web, .gutenberg) |
| 119 | + expec.fulfill() |
| 120 | + }) { (error) in |
| 121 | + XCTFail("This call should succeed. Error: \(error)") |
| 122 | + expec.fulfill() |
| 123 | + } |
| 124 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 125 | + XCTAssertTrue(mockRemoteApi.getMethodCalled) |
| 126 | + |
| 127 | + wait(for: [expec], timeout: 0.1) |
| 128 | + } |
| 129 | + |
| 130 | + func testGetEditorSettingsNotSetForMobile() { |
| 131 | + let expec = expectation(description: "success") |
| 132 | + let response = mockResponse(forMobile: .notSet, andWeb: .gutenberg) |
| 133 | + |
| 134 | + editorServiceRemote.getEditorSettings(siteID, success: { (editor) in |
| 135 | + XCTAssertEqual(editor.mobile, .notSet) |
| 136 | + XCTAssertEqual(editor.web, .gutenberg) |
| 137 | + expec.fulfill() |
| 138 | + }) { (error) in |
| 139 | + XCTFail("This call should succeed. Error: \(error)") |
| 140 | + expec.fulfill() |
| 141 | + } |
| 142 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 143 | + XCTAssertTrue(mockRemoteApi.getMethodCalled) |
| 144 | + |
| 145 | + wait(for: [expec], timeout: 0.1) |
| 146 | + } |
| 147 | + |
| 148 | + func testGetEditorSettingsClassic() { |
| 149 | + let expec = expectation(description: "success") |
| 150 | + let response = mockResponse(forMobile: .aztec, andWeb: .classic) |
| 151 | + |
| 152 | + editorServiceRemote.getEditorSettings(siteID, success: { (editor) in |
| 153 | + XCTAssertEqual(editor.mobile, .aztec) |
| 154 | + XCTAssertEqual(editor.web, .classic) |
| 155 | + expec.fulfill() |
| 156 | + }) { (error) in |
| 157 | + XCTFail("This call should succeed. Error: \(error)") |
| 158 | + expec.fulfill() |
| 159 | + } |
| 160 | + mockRemoteApi.successBlockPassedIn?(response as AnyObject, HTTPURLResponse()) |
| 161 | + XCTAssertTrue(mockRemoteApi.getMethodCalled) |
| 162 | + |
| 163 | + wait(for: [expec], timeout: 0.1) |
| 164 | + } |
| 165 | + |
| 166 | + func testGetEditorSettingsFailure() { |
| 167 | + let expec = expectation(description: "success") |
| 168 | + let errorExpec = NSError(domain: NSURLErrorDomain, code: NSURLErrorUnknown, userInfo: nil) |
| 169 | + |
| 170 | + editorServiceRemote.getEditorSettings(siteID, success: { (editor) in |
| 171 | + XCTFail("This call should error") |
| 172 | + expec.fulfill() |
| 173 | + }) { (error) in |
| 174 | + XCTAssertEqual(error as NSError, errorExpec) |
| 175 | + expec.fulfill() |
| 176 | + } |
| 177 | + mockRemoteApi.failureBlockPassedIn?(errorExpec, nil) |
| 178 | + XCTAssertTrue(mockRemoteApi.getMethodCalled) |
| 179 | + |
| 180 | + wait(for: [expec], timeout: 0.1) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +extension EditorServiceRemoteTests { |
| 185 | + func mockResponse(forMobile mobile: EditorSettings.Mobile, andWeb web: EditorSettings.Web) -> [String: String] { |
| 186 | + return [ |
| 187 | + "editor_mobile": mobile.rawValue, |
| 188 | + "editor_web": web.rawValue, |
| 189 | + ] |
| 190 | + } |
| 191 | +} |
0 commit comments