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

Add fetch example & test #199

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gnclmorais
Copy link

This is one of the situations I’ve used the most with ember-browser-services but I didn’t see it reflected on the documentation… So I whipped up an example and corresponding test to make it clearer for other users.

I ran into an interesting situation on the test, where the function was being bound and I had to think a bit outside the box. Let me know what you think!

window: { location: { href: 'https://crowdstrike.com' } },
window: {
location: { href: 'https://crowdstrike.com' },
fetch: async (url) => ({ url })
Copy link

Choose a reason for hiding this comment

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

Shouldn't the faked version match the signature of the original function?

e.g. shouldn't this return something matching:

interface Response extends Body {
    readonly headers: Headers;
    readonly ok: boolean;
    readonly redirected: boolean;
    readonly status: number;
    readonly statusText: string;
    readonly type: ResponseType;
    readonly url: string;
    clone(): Response;
}

interface Body {
    readonly body: ReadableStream<Uint8Array> | null;
    readonly bodyUsed: boolean;
    arrayBuffer(): Promise<ArrayBuffer>;
    blob(): Promise<Blob>;
    formData(): Promise<FormData>;
    json(): Promise<any>;
    text(): Promise<string>;
}

(from lib.dom)

Obviously you can't have the right body but the url on the Response would enable the test you've written (and maybe in general the sort of tests that you'd want to fake fetch for?)

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, we can make it match 👍 That’s definitely a great improvement! I went with a simple example here, but might as well go all the way and provide a correct example.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gnclmorais if you need/want help, lemme know!

personally, I don't think we should block README updates on something like this, since the underlying implementation is basically YOLO-stubbing

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.

3 participants