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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to test Charts framework elements/protocols #304

Open
asifrazap opened this issue May 25, 2024 · 0 comments
Open

Not able to test Charts framework elements/protocols #304

asifrazap opened this issue May 25, 2024 · 0 comments
Labels
feature request New feature or request

Comments

@asifrazap
Copy link

asifrazap commented May 25, 2024

Hi Team,

I have added Charts framework in SwiftUI with iOS 16.
Tried to test Chart element using Quick and Nimble. Example is given below
I am not able to test ChartContent protocol from Charts framework. Please help ?

import Charts
import SwiftUI

struct Workout: Identifiable {
    let id = UUID()
    let day: String
    let minutes: Int
}

extension Workout {
    static let walkWorkout: [Workout] = [
        .init(day: NSLocalizedString("mon", comment: ""), minutes: 23),
        .init(day: "Tue", minutes: 35),
        .init(day: "Wed", minutes: 55),
        .init(day: "Thu", minutes: 30),
        .init(day: "Fri", minutes: 15),
        .init(day: "Sat", minutes: 65),
        .init(day: "Sun", minutes: 81)
    ]

    static let runWorkout: [Workout] = [
        .init(day: NSLocalizedString("mon", comment: ""), minutes: 16),
        .init(day: "Tue", minutes: 12),
        .init(day: "Wed", minutes: 55),
        .init(day: "Thu", minutes: 34),
        .init(day: "Fri", minutes: 22),
        .init(day: "Sat", minutes: 43),
        .init(day: "Sun", minutes: 90)
    ]
}

struct BarChartExample: View {
    var workoutData = [
        (workoutType: "Walk", data: Workout.walkWorkout),
        (workoutType: "Run", data: Workout.runWorkout)
    ]

    var body: some View {
        Chart {
            BarChartExampleContent(workoutData: workoutData)
        }
        .frame(width: 300, height: 400)
        .chartXAxis {
            AxisMarks { axis in
                AxisValueLabel(centered: true) {
                    Text("\(Workout.walkWorkout[axis.index].day)")
                }
            }
        }
    }
}

struct BarChartExampleContent: ChartContent {
    var workoutData: [(workoutType: String, data: [Workout])]

    var body: some ChartContent {
        ForEach(workoutData, id: \.workoutType) { element in
            ForEach(element.data) {
                BarMark(
                    x: .value("Day", $0.day),
                    y: .value("Workout(in minutes)", $0.minutes)
                )
            }
            .foregroundStyle(by: .value("Workout(type)", element.workoutType))
            .position(by: .value("Workout(type)", element.workoutType))
        }
    }
}

Written test cases as below: -

let workoutData = [
    (workoutType: "Walk", data: Workout.walkWorkout),
    (workoutType: "Run", data: Workout.runWorkout)
]

class BarChartExampleSpec: QuickSpec {
    override func spec() {
        var subject: BarChartExample?
        beforeEach {
            subject = BarChartExample()
            subject?.workoutData = workoutData
        }
        describe("BarChartExample") {
            context("To test Chart with it's child elements") {
                it("check Chart") {
                    let chart = try? subject.inspect().find(Chart<ChartContentStub>.self)
                    expect(chart) != nil
                }
            }
        }
    }
}

class BarChartExampleContentSpec: QuickSpec {
    override func spec() {
        var subject: BarChartExampleContent?
        beforeEach {
            subject = BarChartExampleContent(workoutData: workoutData)
            subject?.workoutData = workoutData
        }
        describe("BarChartExampleContent") {
            context("BarChartContent View") {
                it("check Chart content") {
                    let chart = try? subject.inspect().find(DVBarChartContent.self)     **// Getting error here - Referencing instance method 'inspect(function:)' on 'Optional' requires that 'BarChartExampleContent' conform to 'View'**
                    expect(chart) != nil
                }
            }
        }
    }
}

class ChartContentStub: ChartContent {
    var body: some ChartContent {
        ForEach(workoutData, id: \.workoutType) { element in
            ForEach(element.data) {
                BarMark(
                    x: .value("Day", $0.day),
                    y: .value("Workout(in minutes)", $0.minutes)
                )
            }
            .foregroundStyle(by: .value("Workout(type)", element.workoutType))
            .cornerRadius(10)
            .position(by: .value("Workout(type)", element.workoutType))
        }
    }
}

Getting below error while testing -

Screenshot 2024-05-25 at 8 04 14 AM

Could you please help how to test ChartContent ?

@asifrazap asifrazap changed the title Not able to test Charts framework Not able to test Charts framework elements/protocols May 25, 2024
@nalexn nalexn added the feature request New feature or request label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants