Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Mar 2, 2021
1 parent d82ced9 commit 56b1427
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
3 changes: 1 addition & 2 deletions babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
]
]
}
},
"ignore": ["src/**/*.test.ts"]
}
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"scripts": {
"build": "rimraf dist && yarn build-bundle && yarn build-node && yarn build-web",
"build-bundle": "yarn run webpack",
"build-node": "babel src --out-dir dist/node --extensions .ts && tsc --project tsconfig.build.json --outDir dist/node && rimraf dist/node/**/web* && rimraf dist/node/**/*web*",
"build-web": "babel src --out-dir dist/web --extensions .ts && tsc --project tsconfig.build.json --outDir dist/web && rimraf dist/web/**/node* && rimraf dist/web/**/*node*",
"build-node": "babel src --out-dir dist/node --extensions .ts --ignore 'src/**/*.test.ts' && tsc --project tsconfig.build.json --outDir dist/node && rimraf dist/node/**/web* && rimraf dist/node/**/*web*",
"build-web": "babel src --out-dir dist/web --extensions .ts --ignore 'src/**/*.test.ts' && tsc --project tsconfig.build.json --outDir dist/web && rimraf dist/web/**/node* && rimraf dist/web/**/*node*",
"lint:eslint": "eslint --ext .ts src utils --max-warnings 0",
"lint:tsc": "tsc",
"prepublishOnly": "yarn build",
Expand All @@ -31,10 +31,10 @@
],
"coverageThreshold": {
"global": {
"branches": 98,
"functions": 98,
"lines": 98,
"statements": 98
"branches": 97,
"functions": 97,
"lines": 97,
"statements": 97
}
},
"rootDir": "src"
Expand Down Expand Up @@ -82,7 +82,7 @@
"sjcl": "^1.0.8",
"tweetnacl": "^1.0.3",
"url-join": "^4.0.1",
"url-parse": "^1.4.7"
"url-parse": "1.4.7"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
Expand All @@ -107,7 +107,7 @@
"eslint": "^7.11.0",
"eslint-plugin-compat": "^3.8.0",
"eslint-plugin-jsdoc": "^32.0.0",
"husky": "^5.0.9",
"husky": "^5.1.3",
"jest": "^26.4.2",
"lint-staged": "^10.3.0",
"prettier": "^2.1.1",
Expand Down
50 changes: 23 additions & 27 deletions src/download/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { PassThrough } from "stream";
import axios from "axios";
import tmp from "tmp";
import fs from "fs";

import { SkynetClient, defaultPortalUrl, uriSkynetPrefix } from "../index.node";

jest.mock("axios");
jest.mock("fs");

const portalUrl = defaultPortalUrl();
const skylink = "XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg";
Expand All @@ -18,33 +20,35 @@ const fullHeaders = {
"skynet-file-metadata": JSON.stringify(skynetFileMetadata),
};
const body = "asdf";
const filename = "foo";

describe("downloadFileToPath", () => {
beforeEach(() => {
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: fullHeaders });
const mockWriteable = new PassThrough();
// @ts-expect-error TS complaining.
fs.createWriteStream.mockReturnValueOnce(mockWriteable);
setTimeout(() => {
mockWriteable.emit("finish");
}, 100);
});

it("should send get request to default portal", () => {
const tmpFile = tmp.fileSync();

client.downloadFileToPath(skylink, tmpFile.name);
it("should send get request to default portal", async () => {
await client.downloadFileToPath(skylink, filename);

expect(axios).toHaveBeenCalledWith(
expect.objectContaining({
url: `${portalUrl}/${skylink}`,
method: "get",
})
);

tmpFile.removeCallback();
});

it("should use custom connection options if defined on the client", async () => {
const tmpFile = tmp.fileSync();
const client = new SkynetClient("", { APIKey: "foobar", customUserAgent: "Sia-Agent" });

const { contentType, metadata, skylink: skylink2 } = await client.downloadFileToPath(skylink, tmpFile.name, {
const { contentType, metadata, skylink: skylink2 } = await client.downloadFileToPath(skylink, filename, {
APIKey: "barfoo",
customUserAgent: "Sia-Agent-2",
});
Expand All @@ -60,23 +64,17 @@ describe("downloadFileToPath", () => {
headers: expect.objectContaining({ "User-Agent": "Sia-Agent-2" }),
})
);

tmpFile.removeCallback();
});

it("should fetch info even when headers are missing", async () => {
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: {} });

const tmpFile = tmp.fileSync();

const { contentType, metadata, skylink: skylink2 } = await client.downloadFileToPath(skylink, tmpFile.name);
const { contentType, metadata, skylink: skylink2 } = await client.downloadFileToPath(skylink, filename);

expect(contentType).toEqual("");
expect(metadata).toEqual({});
expect(skylink2).toEqual("");

tmpFile.removeCallback();
});
});

Expand All @@ -86,12 +84,16 @@ describe("downloadFileHnsToPath", () => {
beforeEach(() => {
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: fullHeaders });
const mockWriteable = new PassThrough();
// @ts-expect-error TS complaining.
fs.createWriteStream.mockReturnValueOnce(mockWriteable);
setTimeout(() => {
mockWriteable.emit("finish");
}, 100);
});

it("should send get request to default portal", async () => {
const tmpFile = tmp.fileSync();

const { contentType, metadata, skylink: skylink2 } = await client.downloadFileHnsToPath(domain, tmpFile.name);
it("should send get request for HNS to default portal", async () => {
const { contentType, metadata, skylink: skylink2 } = await client.downloadFileHnsToPath(domain, filename);

expect(contentType).toEqual(skynetfileContentType);
expect(metadata).toEqual(skynetFileMetadata);
Expand All @@ -103,22 +105,16 @@ describe("downloadFileHnsToPath", () => {
method: "get",
})
);

tmpFile.removeCallback();
});

it("should get info when headers are missing", async () => {
// @ts-expect-error TS complaining.
axios.mockResolvedValue({ data: { body, pipe: jest.fn() }, headers: {} });

const tmpFile = tmp.fileSync();

const { contentType, metadata, skylink: skylink2 } = await client.downloadFileHnsToPath(domain, tmpFile.name);
const { contentType, metadata, skylink: skylink2 } = await client.downloadFileHnsToPath(domain, filename);

expect(contentType).toEqual("");
expect(metadata).toEqual({});
expect(skylink2).toEqual("");

tmpFile.removeCallback();
});
});
2 changes: 2 additions & 0 deletions src/upload/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ describe("uploadFile", () => {
const { skylink } = await client.uploadFileFromPath(file.name);

expect(skylink).toEqual(sialink);

file.removeCallback();
});

it("should return skylink on success", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("convertSkylinkToBase32", () => {

describe("formatSkylink", () => {
it("should ensure the skylink starts with the prefix", () => {
const prefixedSkylink = `sia:${skylink}`;
const prefixedSkylink = `sia://${skylink}`;

expect(formatSkylink(skylink)).toEqual(prefixedSkylink);
expect(formatSkylink(prefixedSkylink)).toEqual(prefixedSkylink);
Expand Down
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader",
options: {
ignore: ["src/**/*.test.ts"],
},
},
],
},
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3353,7 +3353,7 @@ human-signals@^2.1.0:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==

husky@^5.0.9:
husky@^5.1.3:
version "5.1.3"
resolved "https://registry.yarnpkg.com/husky/-/husky-5.1.3.tgz#1a0645a4fe3ffc006c4d0d8bd0bcb4c98787cc9d"
integrity sha512-fbNJ+Gz5wx2LIBtMweJNY1D7Uc8p1XERi5KNRMccwfQA+rXlxWNSdUxswo0gT8XqxywTIw7Ywm/F4v/O35RdMg==
Expand Down Expand Up @@ -5953,10 +5953,10 @@ url-join@^4.0.1:
resolved "https://registry.yarnpkg.com/url-join/-/url-join-4.0.1.tgz#b642e21a2646808ffa178c4c5fda39844e12cde7"
integrity sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==

url-parse@^1.4.7:
version "1.5.1"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.1.tgz#d5fa9890af8a5e1f274a2c98376510f6425f6e3b"
integrity sha512-HOfCOUJt7iSYzEx/UqgtwKRMC6EU91NFhsCHMv9oM03VJcVo2Qrp8T8kI9D7amFf1cu+/3CEhgb3rF9zL7k85Q==
[email protected]:
version "1.4.7"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.7.tgz#a8a83535e8c00a316e403a5db4ac1b9b853ae278"
integrity sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==
dependencies:
querystringify "^2.1.1"
requires-port "^1.0.0"
Expand Down

0 comments on commit 56b1427

Please sign in to comment.