Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
add test for image-file
Browse files Browse the repository at this point in the history
- set target to es2019 to get the test to compile
- minor refactor of cache-file so axios response can be mocked
- add .cache and .idea to gitignore

Note: this creates a .cache directory as part of the test
  • Loading branch information
tomconder committed Oct 10, 2020
1 parent d491ce3 commit 652f93c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ node_modules
*.log
!jest.config.js
coverage/
.cache
.idea
77 changes: 77 additions & 0 deletions src/__tests__/image-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import ImageFile from '../image-file';
import {GatsbyCache, NodePluginArgs, Store} from 'gatsby';
import axios, {AxiosResponse} from 'axios';

jest.mock('axios');

describe('image-file', () => {
const state = {
program: {
directory: '.'
}
};

const store: Store = {
dispatch: jest.fn(),
subscribe: jest.fn(),
getState: () => state,
replaceReducer: jest.fn(),
};

const cache: NodePluginArgs["cache"] = new class implements GatsbyCache {
get(): Promise<unknown> {
return Promise.resolve(undefined);
}

set(): Promise<unknown> {
return Promise.resolve(undefined);
}
};

describe('getHref', () => {
let params: {
hasSecret: boolean;
markers: string | string[];
visible: string | string[];
style: string | string[];
path: string | string[];
format: string;
client: string;
};

it('with all params', async () => {
const axiosResponse: AxiosResponse = {
data: {},
status: 200,
statusText: "OK",
config: {},
headers: {}
};

jest.spyOn(axios, "get").mockResolvedValueOnce(axiosResponse);

params = {
hasSecret: false,
markers: "test-markers",
visible: "test-visible",
style: "test-style",
path: "test-path",
format: "test-format",
client: "test-client",
};
const imageFile = new ImageFile(cache, params);

const result = await imageFile.getHref(store, "", "")

expect(result.hash).toMatch(/client=test-client&/);
expect(result.hash).toMatch(/format=test-format&/);
expect(result.hash).toMatch(/hasSecret=false/);
expect(result.hash).toMatch(/markers=test-markers&/);
expect(result.hash).toMatch(/path=test-path&/);
expect(result.hash).toMatch(/style=test-style&/);
expect(result.hash).toMatch(/visible=test-visible$/);

expect(result.path).toMatch(/^.cache/);
});
});
});
13 changes: 5 additions & 8 deletions src/cache-file.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Store, NodePluginArgs } from "gatsby";

import crypto from "crypto";
import Axios, { AxiosInstance, AxiosResponse } from "axios";
import axios, { AxiosResponse } from "axios";

import createFile from "./create-file";

abstract class CacheFile {
protected _path: string | undefined;
protected _type = "";

private _axiosClient: AxiosInstance;
private _cache: NodePluginArgs["cache"];

private _hash = "";
Expand All @@ -22,11 +21,6 @@ abstract class CacheFile {
this.hash = hash;

this.checkCache();

this._axiosClient = Axios.create({
method: "GET",
responseType: "arraybuffer",
});
}

protected async getPath(
Expand Down Expand Up @@ -63,7 +57,10 @@ abstract class CacheFile {
}

private async downloadFile(url: string): Promise<AxiosResponse> {
return await this._axiosClient.get(url);
return await axios.get(url, {
method: "GET",
responseType: "arraybuffer",
});
}

private set hash(newHash: string) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// Target latest version of ECMAScript.
"target": "es2020",
"target": "es2019",
// Enable strictest settings like strictNullChecks & noImplicitAny.
"strict": true,
// Disallow features that require cross-file information for emit.
Expand Down

0 comments on commit 652f93c

Please sign in to comment.