82
loading...
This website collects cookies to deliver better user experience
function makeDate(timestamp: number): Date;
function makeDate(m: number, d: number, y: number): Date;
// src/services/google.ts
import { auth, drive } from '@googleapis/drive';
import config from '../config';
const oAuth2Client = new auth.OAuth2(
config.GOOGLE.client_id,
config.GOOGLE.client_secret
);
oAuth2Client.setCredentials({ refresh_token: config.GOOGLE.refresh_token });
export const googleDrive = drive({ version: 'v3', auth: oAuth2Client });
// src/routes/getFolders
async function getFolders(){
const folders = await googleDrive.files.list({
pageSize: 10,
fields: 'files(id, name)',
q: "mimeType = 'application/vnd.google-apps.folder'",
});
return folders
}
googleDrive.files.list
and return a custom response in order not to hit the actual API. googleDrive.files
class:class Resource$Drives {
// ...
list(
params: Params$Resource$Files$List,
options: StreamMethodOptions
): GaxiosPromise<Readable>;
list(
params?: Params$Resource$Files$List,
options?: MethodOptions
): GaxiosPromise<Schema$FileList>;
list(
params: Params$Resource$Files$List,
options: StreamMethodOptions | BodyResponseCallback<Readable>,
callback: BodyResponseCallback<Readable>
): void;
list(
params: Params$Resource$Files$List,
options: MethodOptions | BodyResponseCallback<Schema$FileList>,
callback: BodyResponseCallback<Schema$FileList>
): void;
list(
params: Params$Resource$Files$List,
callback: BodyResponseCallback<Schema$FileList>
): void;
list(callback: BodyResponseCallback<Schema$FileList>): void;
//...
}
getFolders
returns a Promise. To be precise, the second signature is the one we want:list(
params?: Params$Resource$Files$List,
options?: MethodOptions
): GaxiosPromise<Schema$FileList>;
import { drive_v3,
GaxiosPromise,
MethodOptions } from '@googleapis/drive';
// Mock the module that exports the googleDrive client:
jest.mock('../src/services/google');
// Typing our spy according to the signature that we use
type DriveListSpy = (
params?: drive_v3.Params$Resource$Files$List,
options?: MethodOptions
) => GaxiosPromise<drive_v3.Schema$FileList>;
// Setting the right method overloads manually
const driveListSpy = jest.spyOn(
googleDrive.files,
'list'
) as unknown as jest.MockedFunction<DriveListSpy>;
DriveListSpy
type is more or less copy-paste from the type definition file provided by the library. It may take some time to figure out from where you can import the types. Here, the MethodOptions
interface is a direct named export, the params and Promise result type are part of the drive_v3
export.driveListSpy.mockResolvedValue({
config: {},
data: { files: [{ name: 'folder', id: 'id' }] },
status: 200,
statusText: 'OK',
headers: {},
request: { responseURL: 'test' },
});
it('lists folders', async () => {
await getFolders();
expect(driveListSpy).toHaveBeenCalledTimes(1);
expect(driveListSpy.mock.calls[0][0]).toMatchSnapshot();
});
mockResolvedValueOnce
).as unknown as <type>
assertions are error-prone since we tell the compiler not to infer types and instead use our custom type.