Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test in light and dark mode #265

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

dchohfi
Copy link

@dchohfi dchohfi commented Mar 31, 2023

Hi 馃憢

It is a pleasure to be able to open a PR in this lib, I've been using this for years and years, thank you for creating and supporting it!

I had to add a support for light/dark mode on a private app and thought about opening a PR, lets see if you think this worth to be merged on the main code.

Hopefully I did everything (almost) correct 馃檹

FYI: my last commit - 3fb008d - tries to add a test that wasn't added to the project, and I don't know why it isn't recording snapshots, I might need some help with it.

Anyway, let me know how can I improve this PR and if it makes sense to be added to the main code.

Copy link
Collaborator

@diogot diogot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dchohfi, this looks great!
I add just a few small comments and I would like to know the opinion of @ashfurrow or someone more experienced in one topic before approve, just to be sure.
But great work!

}

it("fails to find the snapshots due to the custom folder") {
expect(view).notTo(haveValidSnapshot(named: "something custom"))
expect(view).notTo(haveValidSnapshot(named: "something custom"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated?

}

it("finds the snapshots using a custom images directory") {
expect(view).to(haveValidSnapshot())
expect(view).to(recordSnapshot())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftovers?

Comment on lines +34 to +39
expect(view).to(recordDeviceAgnosticSnapshot())
}

it("find the snapshot using a custom image directory for light and dark mode") {
expect(view).to(recordSnapshot(userInterfaceStyle: .light))
expect(view).to(recordSnapshot(userInterfaceStyle: .dark))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess all of those also should be haveValid and not record

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't get this test to run properly, we still need to record images for this test, that is why I left it like that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should stick to AAA (A.K.A Arrange, Act, and Assert) principles, and as we expect two different outputs , should be two isolated unit tests

Comment on lines +70 to +71
// if an userInterfaceStyle is sent, we need to force usesDrawViewHierarchyInRect
snapshotController.usesDrawViewHierarchyInRect = userInterfaceStyle == nil ? usesDrawRect : true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to hear what more experienced in the library thinks about it.
I'm not sure about the side effects of using usesDrawViewHierarchyInRect or not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate on this solution, I'm trying to understand why usesDrawViewHierarchyInRect should be always true when an userInterfaceStyle is set

@ashfurrow
Copy link
Owner

Hey this looks cool! Apologies for not reviewing sooner, things have been hectic. I probably won't get a chance to review this until later this week. Thanks the PR and for your patience 馃檹

@dchohfi
Copy link
Author

dchohfi commented Apr 24, 2023

hey @ashfurrow no problem! we're using internally our own fork for now.

let me know when you have time, thank you.

@@ -8,7 +8,13 @@ public final class DynamicTypeView: UIView {
label = UILabel()
super.init(frame: frame)

backgroundColor = .white
backgroundColor = UIColor { traits -> UIColor in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don麓t need the else here

backgroundColor = UIColor { traits -> UIColor in
    if traits.userInterfaceStyle == .dark {
        return .lightGray
    }
    return .white
}

@@ -12,19 +12,31 @@ final class BootstrapCustomFormatTests: QuickSpec {
beforeEach {
setNimbleTestFolder("CustomFolder")
view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 44, height: 44)))
view.backgroundColor = .blue
view.backgroundColor = UIColor { traits -> UIColor in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here about the else

Comment on lines +34 to +39
expect(view).to(recordDeviceAgnosticSnapshot())
}

it("find the snapshot using a custom image directory for light and dark mode") {
expect(view).to(recordSnapshot(userInterfaceStyle: .light))
expect(view).to(recordSnapshot(userInterfaceStyle: .dark))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should stick to AAA (A.K.A Arrange, Act, and Assert) principles, and as we expect two different outputs , should be two isolated unit tests

view.backgroundColor = UIColor { traits -> UIColor in
if traits.userInterfaceStyle == .dark {
return .brown
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about the else statement

}

it("has a valid snapshot") {
expect(view).to(haveValidSnapshot())
expect(view).to(haveValidSnapshot(userInterfaceStyle: .light))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about the expectations

view.backgroundColor = UIColor { traits -> UIColor in
if traits.userInterfaceStyle == .dark {
return .brown
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about the else statement

}

it("has a valid snapshot to all sizes") {
expect(view).to(haveValidDynamicSizeSnapshot(sizes: sizes))
// expect(view).to(recordDynamicSizeSnapshot(sizes: sizes))
}

it("has a valid snapshot to all sizes in light and dark mode") {
expect(view).to(haveValidDynamicSizeSnapshot(sizes: sizes, userInterfaceStyle: .light))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about expectations

Comment on lines +70 to +71
// if an userInterfaceStyle is sent, we need to force usesDrawViewHierarchyInRect
snapshotController.usesDrawViewHierarchyInRect = userInterfaceStyle == nil ? usesDrawRect : true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate on this solution, I'm trying to understand why usesDrawViewHierarchyInRect should be always true when an userInterfaceStyle is set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants