Skip to content

Commit

Permalink
Use non mocked instance for createArtifact and mocked instance for ge…
Browse files Browse the repository at this point in the history
…tArtifact and deleteArtifact as there is an ES error in those methods if not mocked
  • Loading branch information
dasansol92 committed Sep 7, 2021
1 parent 77358c7 commit fe33bb3
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { elasticsearchServiceMock } from '../../../../../../../src/core/server/m
// Because mocks are for testing only, should be ok to import the FleetArtifactsClient directly
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { FleetArtifactsClient } from '../../../../../fleet/server/services';
import { createArtifactsClientMock } from '../../../../../fleet/server/mocks';

export const getManifestClientMock = (
savedObjectsClient?: SavedObjectsClientContract
Expand All @@ -35,17 +36,19 @@ export const createEndpointArtifactClientMock = (
): jest.Mocked<EndpointArtifactClientInterface> & {
_esClient: ElasticsearchClientMock;
} => {
const fleetArtifactClient = new FleetArtifactsClient(esClient, 'endpoint');
const endpointArtifactClient = new EndpointArtifactClient(fleetArtifactClient);
const fleetArtifactClientMocked = createArtifactsClientMock();
const endpointArtifactClientMocked = new EndpointArtifactClient(fleetArtifactClientMocked);

// Return the interface mocked with jest.fn() that fowards calls to the real instance
return {
createArtifact: jest.fn(async (...args) => {
const fleetArtifactClient = new FleetArtifactsClient(esClient, 'endpoint');
const endpointArtifactClient = new EndpointArtifactClient(fleetArtifactClient);
const response = await endpointArtifactClient.createArtifact(...args);
return response;
}),
getArtifact: jest.fn((...args) => endpointArtifactClient.getArtifact(...args)),
deleteArtifact: jest.fn((...args) => endpointArtifactClient.deleteArtifact(...args)),
getArtifact: jest.fn((...args) => endpointArtifactClientMocked.getArtifact(...args)),
deleteArtifact: jest.fn((...args) => endpointArtifactClientMocked.deleteArtifact(...args)),
_esClient: esClient,
};
};

0 comments on commit fe33bb3

Please sign in to comment.