2020-12-17 11:38:21 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2021-05-12 06:35:00 +00:00
|
|
|
const grpcJs = jest.requireActual('@grpc/grpc-js');
|
2020-12-17 11:38:21 +00:00
|
|
|
|
|
|
|
const mockCallWrite = jest.fn();
|
|
|
|
const mockCallEnd = jest.fn();
|
|
|
|
const mockCallCancel = jest.fn();
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export const status = grpcJs.status;
|
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
class MockCall extends EventEmitter {
|
|
|
|
write(...args) {
|
|
|
|
mockCallWrite(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
end(...args) {
|
|
|
|
mockCallEnd(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(...args) {
|
|
|
|
mockCallCancel(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mockCall = new MockCall();
|
|
|
|
|
|
|
|
const makeMockCall = () => {
|
|
|
|
mockCall = new MockCall();
|
|
|
|
};
|
|
|
|
|
|
|
|
const getMockCall = () => mockCall;
|
|
|
|
|
|
|
|
const mockConstructor = jest.fn();
|
|
|
|
const mockMakeUnaryRequest = jest.fn();
|
|
|
|
const mockMakeClientStreamRequest = jest.fn();
|
|
|
|
const mockMakeServerStreamRequest = jest.fn();
|
|
|
|
const mockMakeBidiStreamRequest = jest.fn();
|
|
|
|
const mockCreateInsecure = jest.fn();
|
|
|
|
const mockCreateSsl = jest.fn();
|
|
|
|
|
|
|
|
export const grpcMocks = {
|
|
|
|
getMockCall,
|
|
|
|
mockConstructor,
|
|
|
|
mockMakeUnaryRequest,
|
|
|
|
mockMakeClientStreamRequest,
|
|
|
|
mockMakeServerStreamRequest,
|
|
|
|
mockMakeBidiStreamRequest,
|
|
|
|
mockCreateInsecure,
|
|
|
|
mockCreateSsl,
|
|
|
|
mockCallWrite,
|
|
|
|
mockCallEnd,
|
|
|
|
mockCallCancel,
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockGrpcClient {
|
|
|
|
constructor(...args) {
|
|
|
|
mockConstructor(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
makeUnaryRequest(...args) {
|
|
|
|
mockMakeUnaryRequest(...args);
|
|
|
|
makeMockCall();
|
|
|
|
return getMockCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
makeClientStreamRequest(...args) {
|
|
|
|
mockMakeClientStreamRequest(...args);
|
|
|
|
makeMockCall();
|
|
|
|
return getMockCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
makeServerStreamRequest(...args) {
|
|
|
|
mockMakeServerStreamRequest(...args);
|
|
|
|
makeMockCall();
|
|
|
|
return getMockCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
makeBidiStreamRequest(...args) {
|
|
|
|
mockMakeBidiStreamRequest(...args);
|
|
|
|
makeMockCall();
|
|
|
|
return getMockCall();
|
|
|
|
}
|
2021-12-06 10:12:18 +00:00
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function makeGenericClientConstructor() {
|
|
|
|
return MockGrpcClient;
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:12:18 +00:00
|
|
|
export class Metadata {
|
|
|
|
/**
|
|
|
|
* Mock Metadata class to avoid TypeError: grpc.Metadata is not a constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 11:38:21 +00:00
|
|
|
export const credentials = {
|
|
|
|
createInsecure: mockCreateInsecure,
|
|
|
|
createSsl: mockCreateSsl,
|
|
|
|
};
|