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

E2E: Refactor setup method to support class inheritance in RequestUtils #59362

Merged
merged 1 commit into from
Feb 26, 2024

Conversation

WunderBart
Copy link
Member

This PR updates the setup static method in the RequestUtils class to use this instead of directly referencing the RequestUtils class. This change ensures that when the setup method is called on any class that extends RequestUtils, it correctly returns an instance of the calling class, thereby supporting class inheritance and extending functionality.

Before

class RequestUtils {
    static setup() {
        // Incorrectly returns an instance of the base class directly
        return new RequestUtils();
    }

    // Base class method
    baseMethod() {
        return "Base method";
    }
}

class ExtendedRequestUtils extends RequestUtils {
    // Method only available in the extended class
    extendedMethod() {
        return "Extended method";
    }
}

// Trying to use the extended class method after setup
const instance = ExtendedRequestUtils.setup();
console.log(instance instanceof ExtendedRequestUtils); // Expected true, but it's false
console.log(instance instanceof RequestUtils); // True

// Trying to access the extended method will result in an error
// console.log(instance.extendedMethod()); // TypeError: instance.extendedMethod is not a function

After

class RequestUtils {
    static setup() {
        // Dynamically returns an instance of the class it's called on
        return new this();
    }

    // Base class method
    baseMethod() {
        return "Base method";
    }
}

class ExtendedRequestUtils extends RequestUtils {
    // Method only available in the extended class
    extendedMethod() {
        return "Extended method";
    }
}

// Using the setup method properly now
const correctInstance = ExtendedRequestUtils.setup();
console.log(correctInstance instanceof ExtendedRequestUtils); // True
console.log(correctInstance instanceof RequestUtils); // True

// Accessing the extended method works as expected
console.log(correctInstance.extendedMethod()); // Outputs: "Extended method"

Testing Instructions

All the tests should pass.

Copy link

github-actions bot commented Feb 26, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: WunderBart <bartkalisz@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@WunderBart WunderBart added the [Package] E2E Test Utils /packages/e2e-test-utils label Feb 26, 2024
@Mamaduka Mamaduka added the [Type] Enhancement A suggestion for improvement. label Feb 26, 2024
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Makes sense 👍

@WunderBart WunderBart enabled auto-merge (squash) February 26, 2024 10:52
@WunderBart WunderBart merged commit 372308e into trunk Feb 26, 2024
61 of 64 checks passed
@WunderBart WunderBart deleted the fix/request-utils-constructor-reference branch February 26, 2024 11:09
@github-actions github-actions bot added this to the Gutenberg 17.9 milestone Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] E2E Test Utils /packages/e2e-test-utils [Type] Enhancement A suggestion for improvement.
2 participants