Skip to content

Commit

Permalink
Add unit test for WP.org POST request
Browse files Browse the repository at this point in the history
Apply swiftlint corrections
  • Loading branch information
fluiddot committed Mar 29, 2023
1 parent 404fe65 commit 9f62bb3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
50 changes: 50 additions & 0 deletions WordPressKitTests/Mock Data/wp-reusable-blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": 6,
"date": "2021-02-10T11:51:53",
"date_gmt": "2021-02-10T11:51:53",
"guid": {
"rendered": "https:\/\/test-site.org\/2021\/02\/10\/untitled-reusable-block\/",
"raw": "https:\/\/test-site.org\/2021\/02\/10\/untitled-reusable-block\/"
},
"modified": "2021-02-10T12:31:39",
"modified_gmt": "2021-02-10T12:31:39",
"password": "",
"slug": "untitled-reusable-block",
"status": "publish",
"type": "wp_block",
"link": "https:\/\/test-site.org\/2021\/02\/10\/untitled-reusable-block\/",
"title": {
"raw": "A resuable block"
},
"content": {
"raw": "<!-- wp:paragraph -->\n<p>Some text<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>Item 1<\/li><li>Item 2<\/li><li>Item 3<\/li><\/ul>\n<!-- \/wp:list -->",
"protected": false,
"block_version": 1
},
"template": "",
"_links": {
"self": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks\/6"
}],
"collection": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks"
}],
"about": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/types\/wp_block"
}],
"wp:attachment": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/media?parent=6"
}],
"wp:action-publish": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks\/6"
}],
"wp:action-unfiltered-html": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks\/6"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
}
}
27 changes: 26 additions & 1 deletion WordPressKitTests/WordPressOrgRestApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WordPressOrgRestApiTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testSuccessfulCall() {
func testSuccessfulGetCall() {
stub(condition: isAPIRequest()) { _ in
let stubPath = OHPathForFile("wp-pages.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -63,4 +63,29 @@ class WordPressOrgRestApiTests: XCTestCase {
}
waitForExpectations(timeout: 2, handler: nil)
}

func testSuccessfulPostCall() {
stub(condition: isAPIRequest()) { _ in
let stubPath = OHPathForFile("wp-reusable-blocks.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}
let expect = self.expectation(description: "One callback should be invoked")
let api = WordPressOrgRestApi(apiBase: apiBase)
let blockContent = "<!-- wp:paragraph --><p>Some text</p><!-- /wp:paragraph --><!-- wp:list --><ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul><!-- /wp:list -->"
let parameters: [String: String] = ["id": "6", "content": blockContent]
api.POST("wp/v2/blocks/6", parameters: parameters as [String: AnyObject]) { (result, _) in
expect.fulfill()
switch result {
case .success(let object):
guard let block = object as? [String: AnyObject] else {
XCTFail("Unexpected API result")
return
}
XCTAssertEqual(block.count, 15, "The API should return the block")
case .failure:
XCTFail("This call should not fail")
}
}
waitForExpectations(timeout: 2, handler: nil)
}
}

0 comments on commit 9f62bb3

Please sign in to comment.