Skip to content

Commit

Permalink
restore describe block ending to where it was previously
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Dec 9, 2022
1 parent 52a1ee5 commit fc82dc5
Showing 1 changed file with 86 additions and 86 deletions.
172 changes: 86 additions & 86 deletions src/__tests__/RESTDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,107 +1206,107 @@ describe('RESTDataSource', () => {
// `set-cookie` with `shared: true`. (Note the `.times(2)` above.)
await dataSource.getFoo(2, true);
});
});

describe('user hooks', () => {
describe('willSendRequest', () => {
const obj = { foo: 'bar' };
const str = 'foo=bar';
const buffer = Buffer.from(str);

it.each([
['object', obj, obj],
['string', str, str],
['buffer', buffer, str],
])(`can set the body to a %s`, async (_, body, expected) => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: string | Buffer | { foo: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override async willSendRequest(
path: string,
requestOpts: AugmentedRequest,
) {
expect(path).toMatch('foo/1');
expect(requestOpts.body).toEqual(body);
}
})();

nock(apiUrl).post('/foo/1', expected).reply(200);
await dataSource.updateFoo(1, body);
});
describe('user hooks', () => {
describe('willSendRequest', () => {
const obj = { foo: 'bar' };
const str = 'foo=bar';
const buffer = Buffer.from(str);

it.each([
['object', obj, obj],
['string', str, str],
['buffer', buffer, str],
])(`can set the body to a %s`, async (_, body, expected) => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: string | Buffer | { foo: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override async willSendRequest(
path: string,
requestOpts: AugmentedRequest,
) {
expect(path).toMatch('foo/1');
expect(requestOpts.body).toEqual(body);
}
})();

nock(apiUrl).post('/foo/1', expected).reply(200);
await dataSource.updateFoo(1, body);
});

it('is called with the correct path', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;
it('is called with the correct path', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: { foo: string }) {
return this.post(`foo/${id}`, { body: foo });
}
updateFoo(id: number, foo: { foo: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override async willSendRequest(
path: string,
_requestOpts: AugmentedRequest,
) {
expect(path).toMatch('foo/1');
}
})();
override async willSendRequest(
path: string,
_requestOpts: AugmentedRequest,
) {
expect(path).toMatch('foo/1');
}
})();

nock(apiUrl).post('/foo/1', obj).reply(200);
await dataSource.updateFoo(1, obj);
});
nock(apiUrl).post('/foo/1', obj).reply(200);
await dataSource.updateFoo(1, obj);
});
});

describe('resolveURL', () => {
it('sees the same request body as provided by the caller', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;
describe('resolveURL', () => {
it('sees the same request body as provided by the caller', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: { name: string }) {
return this.post(`foo/${id}`, { body: foo });
}
updateFoo(id: number, foo: { name: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override resolveURL(path: string, requestOpts: AugmentedRequest) {
expect(requestOpts.body).toMatchInlineSnapshot(`
override resolveURL(path: string, requestOpts: AugmentedRequest) {
expect(requestOpts.body).toMatchInlineSnapshot(`
{
"name": "blah",
}
`);
return super.resolveURL(path, requestOpts);
}
})();

nock(apiUrl)
.post('/foo/1', JSON.stringify({ name: 'blah' }))
.reply(200);
await dataSource.updateFoo(1, { name: 'blah' });
});
return super.resolveURL(path, requestOpts);
}
})();

nock(apiUrl)
.post('/foo/1', JSON.stringify({ name: 'blah' }))
.reply(200);
await dataSource.updateFoo(1, { name: 'blah' });
});
});

describe('shouldJSONSerializeBody', () => {
it('can be overridden', async () => {
let calls = 0;
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: { name: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override shouldJSONSerializeBody(
body: string | object | Buffer | undefined,
) {
calls++;
return super.shouldJSONSerializeBody(body);
}
})();

nock(apiUrl).post('/foo/1', { name: 'bar' }).reply(200);
await dataSource.updateFoo(1, { name: 'bar' });
expect(calls).toBe(1);
});
describe('shouldJSONSerializeBody', () => {
it('can be overridden', async () => {
let calls = 0;
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: { name: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override shouldJSONSerializeBody(
body: string | object | Buffer | undefined,
) {
calls++;
return super.shouldJSONSerializeBody(body);
}
})();

nock(apiUrl).post('/foo/1', { name: 'bar' }).reply(200);
await dataSource.updateFoo(1, { name: 'bar' });
expect(calls).toBe(1);
});
});
});
Expand Down

0 comments on commit fc82dc5

Please sign in to comment.