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

Extension methods for MockBuilder #5417

Closed
satanTime opened this issue Apr 10, 2023 Discussed in #5404 · 11 comments · Fixed by #6003
Closed

Extension methods for MockBuilder #5417

satanTime opened this issue Apr 10, 2023 Discussed in #5404 · 11 comments · Fixed by #6003
Assignees
Labels
enhancement New feature or request

Comments

@satanTime
Copy link
Member

Discussed in #5404

Originally posted by AE1NS April 6, 2023
I would love to write some reusable extension methods for the MockBuilder. But as MockBuilder is a function and not a class, I'm struggling a bit how to achieve this.
Thought I could use something like this:
.d.ts:

export {};

declare module 'ng-mocks' {
    interface IMockBuilder {
        mockCustom(): IMockBuilder;
    }
}

.ts:

MockBuilder.prototype.mockCustom = function(): IMockBuilder {
    // some custom stuff on the MockBuilder
    return this;
};

test.spec.ts:

beforeEach(() =>
    MockBuilder(TestComponent)
        .mockCustom()
);

The type is recognized correctly but it will not run, as 'MockBuilder.prototype' cant work on a function. Can you tell me if can extend it on another way externally?

@satanTime satanTime added the enhancement New feature or request label Apr 10, 2023
@satanTime satanTime self-assigned this Apr 10, 2023
@AE1NS
Copy link
Contributor

AE1NS commented Apr 11, 2023

There are at least this two options to achieve it:

  • Refactor MockBuilder to a class. But then it wouldnt be possible to use it statically, so it must be called with 'new MockBuilder(' or with a static method, something like 'MockBuilder.create(', that creates the instance then.
  • Create another base class between, i.e. MockBuilder returns a MockBuilderBase (or whatever), class MockBuilderBase extends MockBuilderPerformance, ... and MockBuilderBase must be exported to be accessible by the users. Alternatively just export MockBuilderPerformance, but the name is maybe a bit confusing for others.

What do you think?

At the moment I solved it with a workaround:

export function CustomMockBuilder(...args: Array<MockBuilderParam | MockBuilderParam[] | null | undefined>): IMockBuilder {
    const instance = MockBuilder(...args) as any;

    Object.assign(instance, {
        mockCustom: function (overrides?: Partial<CapacitorGlobal>): IMockBuilder {
            this.mock(...);
            return this;
        },
    });

    return instance;
}

And then Im using the CustomMockBuilder instead of the MockBuilder. But I would prefer a solution with extension methods when it would be possible :P

@satanTime
Copy link
Member Author

THank you for the info. That makes sense. I'll add its support during next weeks.

@AE1NS
Copy link
Contributor

AE1NS commented Jun 5, 2023

Do you have updates on this?

@satanTime
Copy link
Member Author

satanTime commented Jun 11, 2023

Hi @AE1NS,

happy Sunday!

Would something like that work for you?

declare module 'ng-mocks' {
  interface IMockBuilder {
    customMock(value: string): this;
  }
}

MockBuilder.extend('customMock', (builder, [value]: Parameters<IMockBuilder['customMock']>) => {
  // here can be any custom logic around `builder`.
  builder.mock(TargetService, {
    echo: () => value,
  });
});

satanTime added a commit that referenced this issue Jun 11, 2023
feat(MockBuilder): can be extended with custom methods #5417
@satanTime
Copy link
Member Author

@AE1NS
Copy link
Contributor

AE1NS commented Jun 12, 2023

Wow looks great! Thank you!

Btw: Little typo in the documentation inside the example code comment: the extention to MockBuilder <- extension

@satanTime
Copy link
Member Author

Hi there.

If you mean Extension - that's expected. I needed to add a new interface to pass type checks.

Please, let me know how it works for you.

@AE1NS
Copy link
Contributor

AE1NS commented Jun 12, 2023

Just tested it, works like expected!

I am talking about the typo in the comment .customMock('value') // <-- the extention to MockBuilder
where 'extention' should be 'extension'

@satanTime
Copy link
Member Author

Ah. I see. Thank you for spotting that. Could you submit a PR? You can edit the page right on GitHub.

@AE1NS
Copy link
Contributor

AE1NS commented Jun 12, 2023

#6005

@satanTime
Copy link
Member Author

Thank you! I'm going to deploy it later today.

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

Successfully merging a pull request may close this issue.

2 participants