Skip to content

Commit

Permalink
test(extend): add additional function test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mfulton26 committed Jul 17, 2024
1 parent 184bf19 commit 64843f4
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ test('ky.create() does not mangle search params', async t => {
await server.close();
});

test('ky.extend()', async t => {
const extendHooksMacro = test.macro<[{useFunction: boolean}]>(async (t, {useFunction}) => {
const server = await createHttpTestServer();
server.get('/', (_request, response) => {
response.end();
Expand All @@ -551,25 +551,28 @@ test('ky.extend()', async t => {
let isOriginBeforeRequestTrigged = false;
let isExtendBeforeRequestTrigged = false;

const intermediateOptions = {
hooks: {
beforeRequest: [
() => {
isOriginBeforeRequestTrigged = true;
},
],
},
};
const extendedOptions = {
hooks: {
beforeRequest: [
() => {
isExtendBeforeRequestTrigged = true;
},
],
},
};

const extended = ky
.extend({
hooks: {
beforeRequest: [
() => {
isOriginBeforeRequestTrigged = true;
},
],
},
})
.extend({
hooks: {
beforeRequest: [
() => {
isExtendBeforeRequestTrigged = true;
},
],
},
});
.extend(useFunction ? () => intermediateOptions : intermediateOptions)
.extend(useFunction ? () => extendedOptions : extendedOptions);

await extended(server.url);

Expand All @@ -582,7 +585,11 @@ test('ky.extend()', async t => {
await server.close();
});

test('ky.extend() with function', async t => {
test('ky.extend() appends hooks', extendHooksMacro, {useFunction: false});

test('ky.extend() with function appends hooks', extendHooksMacro, {useFunction: false});

test('ky.extend() with function overrides primitives in parent defaults', async t => {
const server = await createHttpTestServer();
server.get('*', (request, response) => {
response.end(request.url);
Expand All @@ -607,6 +614,31 @@ test('ky.extend() with function', async t => {
await server.close();
});

test('ky.extend() with function retains parent defaults when not specified', async t => {
const server = await createHttpTestServer();
server.get('*', (request, response) => {
response.end(request.url);
});

const api = ky.create({prefixUrl: `${server.url}/api`});
const extendedApi = api.extend(() => ({}));

t.is(await api.get('version').text(), '/api/version');
t.is(await extendedApi.get('something').text(), '/api/something');

{
const {ok} = await api.head(server.url);
t.true(ok);
}

{
const {ok} = await extendedApi.head(server.url);
t.true(ok);
}

await server.close();
});

test('throws DOMException/Error with name AbortError when aborted by user', async t => {
const server = await createHttpTestServer();
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down

0 comments on commit 64843f4

Please sign in to comment.