-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MarkOrderAsReadUseCase tests #13946
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
04462e1
Add MarkOrderAsReadUseCaseTests template
bozidarsevo d588979
Merge branch 'trunk' into issue/13851-MarkOrderAsReadUseCase-tests
bozidarsevo f814983
Merge branch 'trunk' into issue/13851-MarkOrderAsReadUseCase-tests
bozidarsevo 54eb6f2
Merge branch 'trunk' into issue/13851-MarkOrderAsReadUseCase-tests
bozidarsevo 046b0f3
Add sample notification in json file used for marking notification as…
bozidarsevo 9c888c0
Update MarkOrderAsReadUseCase.swift
bozidarsevo 4594dbb
Update MarkOrderAsReadUseCaseTests with proper testing
bozidarsevo 023af88
Update total number of sample notifications
bozidarsevo 8c773b9
Merge branch 'trunk' into issue/13851-MarkOrderAsReadUseCase-tests
bozidarsevo a4a42be
Update tests
bozidarsevo 31f5303
Add notificationLoadAllJSONCount magic number
bozidarsevo b086a7f
Clean up MarkOrderAsReadUseCaseTests
bozidarsevo e20e453
Update MarkOrderAsReadUseCaseTests.swift
bozidarsevo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7671,6 +7671,102 @@ | |
} | ||
] | ||
}, | ||
{ | ||
"id": 100041, | ||
"note_hash": 987654, | ||
"type": "store_order", | ||
"read": false, | ||
"noticon": "\uf447", | ||
"timestamp": "2018-10-22T15:29:36+00:00", | ||
"icon": "https:\/\/s.wp.com\/wp-content\/mu-plugins\/notes\/images\/update-payment-2x.png", | ||
"url": "https:\/\/aaaaaaaaa.mystagingwebsite.com\/wp-admin\/post.php?post=1476&action=edit", | ||
"subject": [{ | ||
"text": "\ud83c\udf89 You have a new order!" | ||
}, { | ||
"text": "Someone placed a $20.00 order from Bananza World", | ||
"ranges": [{ | ||
"type": "site", | ||
"indices": [35, 48], | ||
"url": "https:\/\/somewhere.tld", | ||
"id": 123456 | ||
}] | ||
}], | ||
"body": [{ | ||
"text": "", | ||
"media": [{ | ||
"type": "image", | ||
"indices": [0, 0], | ||
"height": "48", | ||
"width": "48", | ||
"url": "https:\/\/s.wp.com\/wp-content\/mu-plugins\/notes\/images\/store-cart-icon.png" | ||
}] | ||
}, { | ||
"text": "Order Number: 1476\nDate: October 22, 2018\nTotal: $20.00\nPayment Method: Credit Card (Stripe)\nShipping Method: Free shipping" | ||
}, { | ||
"text": "Products:\n\nAlmonds \u00d7 1\n" | ||
}, { | ||
"text": "\u2139\ufe0f View Order", | ||
"ranges": [{ | ||
"url": "https:\/\/somewhere.tld\/wp-admin\/post.php?post=1476&action=edit", | ||
"indices": [0, 13], | ||
"type": "link" | ||
}] | ||
}], | ||
"meta": { | ||
"ids": { | ||
"order": 123456 | ||
} | ||
}, | ||
"title": "New Order" | ||
}, | ||
{ | ||
"id": 100042, | ||
"note_hash": 987654, | ||
"type": "store_order", | ||
"read": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read note with order id |
||
"noticon": "\uf447", | ||
"timestamp": "2018-10-22T15:29:36+00:00", | ||
"icon": "https:\/\/s.wp.com\/wp-content\/mu-plugins\/notes\/images\/update-payment-2x.png", | ||
"url": "https:\/\/aaaaaaaaa.mystagingwebsite.com\/wp-admin\/post.php?post=1476&action=edit", | ||
"subject": [{ | ||
"text": "\ud83c\udf89 You have a new order!" | ||
}, { | ||
"text": "Someone placed a $20.00 order from Bananza World", | ||
"ranges": [{ | ||
"type": "site", | ||
"indices": [35, 48], | ||
"url": "https:\/\/somewhere.tld", | ||
"id": 123456 | ||
}] | ||
}], | ||
"body": [{ | ||
"text": "", | ||
"media": [{ | ||
"type": "image", | ||
"indices": [0, 0], | ||
"height": "48", | ||
"width": "48", | ||
"url": "https:\/\/s.wp.com\/wp-content\/mu-plugins\/notes\/images\/store-cart-icon.png" | ||
}] | ||
}, { | ||
"text": "Order Number: 1476\nDate: October 22, 2018\nTotal: $20.00\nPayment Method: Credit Card (Stripe)\nShipping Method: Free shipping" | ||
}, { | ||
"text": "Products:\n\nAlmonds \u00d7 1\n" | ||
}, { | ||
"text": "\u2139\ufe0f View Order", | ||
"ranges": [{ | ||
"url": "https:\/\/somewhere.tld\/wp-admin\/post.php?post=1476&action=edit", | ||
"indices": [0, 13], | ||
"type": "link" | ||
}] | ||
}], | ||
"meta": { | ||
"ids": { | ||
"order": 123456 | ||
} | ||
}, | ||
"title": "New Order" | ||
}, | ||
{ | ||
"id": 8401476681, | ||
"type": "blaze_approved_note", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
WooCommerce/WooCommerceTests/Model/MarkOrderAsReadUseCaseTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import XCTest | ||
import WooFoundation | ||
@testable import Yosemite | ||
@testable import Networking | ||
@testable import Storage | ||
|
||
final class MarkOrderAsReadUseCaseTests: XCTestCase { | ||
private var dispatcher: Dispatcher! | ||
private var network: MockNetwork! | ||
private var storageManager: MockStorageManager! | ||
private var storesManager: MockStoresManager! | ||
private var viewStorage: StorageType { | ||
return storageManager.viewStorage | ||
} | ||
private lazy var sampleNotes: [Yosemite.Note] = { | ||
return try! mapNotes(from: "notifications-load-all") | ||
}() | ||
|
||
private func sampleNote(read: Bool) -> Yosemite.Note? { | ||
return sampleNotes.first { note in | ||
return note.read == read && note.meta.identifier(forKey: .order) != nil | ||
} | ||
} | ||
|
||
override func setUp() { | ||
super.setUp() | ||
dispatcher = Dispatcher() | ||
storageManager = MockStorageManager() | ||
storesManager = MockStoresManager(sessionManager: .makeForTesting()) | ||
network = MockNetwork() | ||
|
||
NotificationStore.resetSharedDerivedStorage() | ||
} | ||
|
||
override func tearDown() { | ||
NotificationStore.resetSharedDerivedStorage() | ||
super.tearDown() | ||
} | ||
|
||
private func setupStoreManagerReceivingNotificationActions(for note: Yosemite.Note, noteStore: NotificationStore) { | ||
storesManager.whenReceivingAction(ofType: NotificationAction.self) { action in | ||
switch action { | ||
case let .synchronizeNotifications(onCompletion): | ||
onCompletion(nil) | ||
case let .synchronizeNotification(_, onCompletion): | ||
onCompletion(note, nil) | ||
case let .updateReadStatus(noteID, read, onCompletion): | ||
noteStore.updateLocalNoteReadStatus(for: [noteID], read: read) { | ||
onCompletion(nil) | ||
} | ||
default: | ||
XCTFail("Unexpected action: \(action)") | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
func test_markOrderNoteAsReadIfNeeded_with_stores_unreadNote() async throws { | ||
let unreadNote = try XCTUnwrap(sampleNote(read: false)) | ||
let orderID = try XCTUnwrap(unreadNote.meta.identifier(forKey: .order)) | ||
|
||
let noteStore = NotificationStore(dispatcher: dispatcher, storageManager: storageManager, network: network) | ||
|
||
setupStoreManagerReceivingNotificationActions(for: unreadNote, noteStore: noteStore) | ||
|
||
await withCheckedContinuation { continuation in | ||
noteStore.updateLocalNotes(with: [unreadNote]) { | ||
XCTAssertEqual(self.viewStorage.countObjects(ofType: Storage.Note.self), 1) | ||
continuation.resume() | ||
} | ||
} | ||
|
||
let result = await MarkOrderAsReadUseCase.markOrderNoteAsReadIfNeeded(stores: storesManager, noteID: unreadNote.noteID, orderID: orderID) | ||
switch result { | ||
case .success(let markedNote): | ||
XCTAssertEqual(unreadNote.noteID, markedNote.noteID) | ||
let storageNote = viewStorage.loadNotification(noteID: markedNote.noteID) | ||
XCTAssertEqual(storageNote?.read, true) | ||
case .failure(let error): | ||
XCTFail(error.localizedDescription) | ||
} | ||
} | ||
|
||
@MainActor | ||
func test_markOrderNoteAsReadIfNeeded_with_stores_alreadyReadNote() async throws { | ||
let readNote = try XCTUnwrap(sampleNote(read: true)) | ||
let orderID = try XCTUnwrap(readNote.meta.identifier(forKey: .order)) | ||
|
||
let noteStore = NotificationStore(dispatcher: dispatcher, storageManager: storageManager, network: network) | ||
|
||
setupStoreManagerReceivingNotificationActions(for: readNote, noteStore: noteStore) | ||
|
||
await withCheckedContinuation { continuation in | ||
noteStore.updateLocalNotes(with: [readNote]) { | ||
XCTAssertEqual(self.viewStorage.countObjects(ofType: Storage.Note.self), 1) | ||
continuation.resume() | ||
} | ||
} | ||
|
||
let result = await MarkOrderAsReadUseCase.markOrderNoteAsReadIfNeeded(stores: storesManager, noteID: readNote.noteID, orderID: orderID) | ||
switch result { | ||
case .success: | ||
XCTFail("Note was already read, it should not be marked as read again.") | ||
case .failure(let error): | ||
if case MarkOrderAsReadUseCase.Error.noNeedToMarkAsRead = error {} else { | ||
XCTFail("Got wrong error \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
func test_markOrderNoteAsReadIfNeeded_with_network_unreadNote() async throws { | ||
let unreadNote = try XCTUnwrap(sampleNote(read: false)) | ||
let orderID = try XCTUnwrap(unreadNote.meta.identifier(forKey: .order)) | ||
|
||
network.simulateResponse(requestUrlSuffix: "notifications", filename: "notifications-load-all") | ||
network.simulateResponse(requestUrlSuffix: "notifications/read", filename: "generic_success") | ||
|
||
let result = await MarkOrderAsReadUseCase.markOrderNoteAsReadIfNeeded(network: network, | ||
noteID: unreadNote.noteID, | ||
orderID: orderID) | ||
|
||
switch result { | ||
case .success(let markedNoteID): | ||
XCTAssertEqual(unreadNote.noteID, markedNoteID) | ||
case .failure(let error): | ||
XCTFail(error.localizedDescription) | ||
} | ||
} | ||
|
||
@MainActor | ||
func test_markOrderNoteAsReadIfNeeded_with_network_alreadyReadNote() async throws { | ||
let readNote = try XCTUnwrap(sampleNote(read: true)) | ||
let orderID = try XCTUnwrap(readNote.meta.identifier(forKey: .order)) | ||
|
||
network.simulateResponse(requestUrlSuffix: "notifications", filename: "notifications-load-all") | ||
|
||
let result = await MarkOrderAsReadUseCase.markOrderNoteAsReadIfNeeded(network: network, | ||
noteID: readNote.noteID, | ||
orderID: orderID) | ||
|
||
switch result { | ||
case .success: | ||
XCTFail("Note was already read, it should not be marked as read again.") | ||
case .failure(let error): | ||
if case MarkOrderAsReadUseCase.Error.noNeedToMarkAsRead = error {} else { | ||
XCTFail("Got wrong error \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Private Methods. | ||
/// | ||
private extension MarkOrderAsReadUseCaseTests { | ||
|
||
/// Returns the NoteListMapper output upon receiving `filename` (Data Encoded) | ||
/// | ||
func mapNotes(from filename: String) throws -> [Yosemite.Note] { | ||
let response = Loader.contentsOf(filename)! | ||
return try NoteListMapper().map(response: response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unread note with order id