Skip to content

Commit 2f7a9a3

Browse files
Added testing documentation
1 parent e466489 commit 2f7a9a3

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,46 @@ Your service worker will receive this message, decode it, and present it to the
373373
374374
### Testing
375375
376-
The `WebPushTesting` module can be used to obtain a mocked `WebPushManager` instance that allows you to capture all messages that are sent out, or throw your own errors to validate your code functions appropriately. Only import `WebPushTesting` in your testing targets.
376+
The `WebPushTesting` module can be used to obtain a mocked `WebPushManager` instance that allows you to capture all messages that are sent out, or throw your own errors to validate your code functions appropriately.
377377
378+
> [!IMPORTANT]
379+
> Only import `WebPushTesting` in your testing targets.
380+
381+
```swift
382+
import Testing
383+
import WebPushTesting
384+
385+
@Test func sendSuccessfulNotifications() async throws {
386+
try await confirmation { requestWasMade in
387+
let mockedManager = WebPushManager.makeMockedManager { message, subscriber, topic, expiration, urgency in
388+
#expect(message.string == "hello")
389+
#expect(subscriber.endpoint.absoluteString == "https://example.com/expectedSubscriber")
390+
#expect(subscriber.vapidKeyID == .mockedKeyID1)
391+
#expect(topic == nil)
392+
#expect(expiration == .recommendedMaximum)
393+
#expect(urgency == .high)
394+
requestWasMade()
395+
}
396+
397+
let myController = MyController(pushManager: mockedManager)
398+
try await myController.sendNotifications()
399+
}
400+
}
401+
402+
@Test func catchBadSubscriptions() async throws {
403+
/// Mocked managers accept multiple handlers, and will cycle through them each time a push message is sent:
404+
let mockedManager = WebPushManager.makeMockedManager(messageHandlers:
405+
{ _, _, _, _, _ in throw BadSubscriberError() },
406+
{ _, _, _, _, _ in },
407+
{ _, _, _, _, _ in throw BadSubscriberError() },
408+
)
409+
410+
let myController = MyController(pushManager: mockedManager)
411+
#expect(myController.subscribers.count == 3)
412+
try await myController.sendNotifications()
413+
#expect(myController.subscribers.count == 1)
414+
}
415+
```
378416
379417
## Specifications
380418

0 commit comments

Comments
 (0)