Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira committed Jan 4, 2024
1 parent 2c0c53d commit d16e029
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 4 additions & 2 deletions UnitTests/PinnedTabs/PinnedTabsViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,19 @@ class PinnedTabsViewModelTests: XCTestCase {
model.fireproof(tabA)
model.removeFireproofing(tabB)
model.close(tabA)
model.muteOrUmute(tabB)

cancellable.cancel()

XCTAssertEqual(events.count, 6)
XCTAssertEqual(events.count, 7)

guard case .bookmark(tabA) = events[0],
case .unpin(1) = events[1],
case .duplicate(0) = events[2],
case .fireproof(tabA) = events[3],
case .removeFireproofing(tabB) = events[4],
case .close(0) = events[5]
case .close(0) = events[5],
case .muteOrUnmute(tabB) = events[6]
else {
XCTFail("Incorrect context menu action")
return
Expand Down
7 changes: 6 additions & 1 deletion UnitTests/TabBar/View/MockTabViewItemDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Foundation
class MockTabViewItemDelegate: TabBarViewItemDelegate {

var hasItemsToTheRight = false
var audioState: WKWebView.AudioState = .notSupported

func tabBarViewItem(_ tabBarViewItem: DuckDuckGo_Privacy_Browser.TabBarViewItem, isMouseOver: Bool) {

Expand Down Expand Up @@ -76,7 +77,7 @@ class MockTabViewItemDelegate: TabBarViewItemDelegate {
}

func tabBarViewItemAudioState(_ tabBarViewItem: TabBarViewItem) -> WKWebView.AudioState {
return .notSupported
return audioState
}

func tabBarViewItemMuteUnmuteSite(_ tabBarViewItem: TabBarViewItem) {
Expand All @@ -87,4 +88,8 @@ class MockTabViewItemDelegate: TabBarViewItemDelegate {
OtherTabBarViewItemsState(hasItemsToTheLeft: false, hasItemsToTheRight: hasItemsToTheRight)
}

func clear() {
self.audioState = .notSupported
}

}
22 changes: 22 additions & 0 deletions UnitTests/TabBar/View/TabBarViewItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ final class TabBarViewItemTests: XCTestCase {
tabBarViewItem.delegate = delegate
}

override func tearDown() {
delegate.clear()
}

func testThatAllExpectedItemsAreShown() {
tabBarViewItem.menuNeedsUpdate(menu)

Expand All @@ -48,6 +52,24 @@ final class TabBarViewItemTests: XCTestCase {
XCTAssertEqual(menu.item(at: 9)?.title, UserText.moveTabToNewWindow)
}

func testThatMuteIsShownWhenCurrentAudioStateIsUnmuted() {
delegate.audioState = .unmuted
tabBarViewItem.menuNeedsUpdate(menu)

XCTAssertTrue(menu.item(at: 5)?.isSeparatorItem ?? false)
XCTAssertEqual(menu.item(at: 6)?.title, UserText.muteTab)
XCTAssertTrue(menu.item(at: 7)?.isSeparatorItem ?? false)
}

func testThatUnmuteIsShownWhenCurrentAudioStateIsMuted() {
delegate.audioState = .muted
tabBarViewItem.menuNeedsUpdate(menu)

XCTAssertTrue(menu.item(at: 5)?.isSeparatorItem ?? false)
XCTAssertEqual(menu.item(at: 6)?.title, UserText.unmuteTab)
XCTAssertTrue(menu.item(at: 7)?.isSeparatorItem ?? false)
}

func testWhenOneTabCloseThenOtherTabsItemIsDisabled() {
tabBarViewItem.menuNeedsUpdate(menu)

Expand Down

0 comments on commit d16e029

Please sign in to comment.