diff --git a/test.app/src/index.html b/test.app/src/index.html new file mode 100644 index 0000000..6042997 --- /dev/null +++ b/test.app/src/index.html @@ -0,0 +1,25 @@ + + + + + + +
+
+ 1. Obtain IP address using GET: https://api.ipify.org +
+
-
+
+ + diff --git a/test.app/src/index.js b/test.app/src/index.js new file mode 100644 index 0000000..7c5bd76 --- /dev/null +++ b/test.app/src/index.js @@ -0,0 +1,20 @@ +const qs = require("qs") +const { GET, setup } = require("../../src") + +setup({ + stringifyQueryParams: source => qs.stringify(source), +}) + +GET("https://api.ipify.org", { + query: { + format: "json", + }, +}) + .then(data => { + document.querySelector("#get-result").innerHTML = `OK ${ + data.ip.split(".").length + }` + }) + .catch(error => { + document.querySelector("#get").innerHTML = error.message + }) diff --git a/test.app/webpack.config.js b/test.app/webpack.config.js new file mode 100644 index 0000000..83dcab6 --- /dev/null +++ b/test.app/webpack.config.js @@ -0,0 +1,47 @@ +/* eslint-env node */ + +const path = require("path") +const HtmlWebPackPlugin = require("html-webpack-plugin") + +module.exports = { + entry: "./test.app/src/index.js", + + output: { + publicPath: "/", + path: path.resolve(__dirname, "dist"), + filename: "[name].js", + }, + + devtool: false, + + module: { + rules: [ + { + test: /.js$/, + exclude: /node_modules/, + use: { + loader: "babel-loader", + }, + }, + ], + }, + + plugins: [ + new HtmlWebPackPlugin({ + template: "test.app/src/index.html", + }), + ], + + optimization: { + runtimeChunk: "single", + splitChunks: { + cacheGroups: { + vendor: { + test: /[/\\]node_modules[/\\]/, + name: "vendors", + chunks: "all", + }, + }, + }, + }, +} diff --git a/test.scenarios/GET.test.js b/test.scenarios/GET.test.js new file mode 100644 index 0000000..ff5daf7 --- /dev/null +++ b/test.scenarios/GET.test.js @@ -0,0 +1,6 @@ +import { Selector as $ } from "testcafe" + +fixture(`asd14/fetch-browser`).page(`http://localhost:50123`) + +test("GET: 200 with json response", t => + t.expect($("#get-result").textContent).eql("OK 4")) diff --git a/test.scenarios/index.js b/test.scenarios/index.js new file mode 100644 index 0000000..0da6e57 --- /dev/null +++ b/test.scenarios/index.js @@ -0,0 +1,32 @@ +/* eslint-disable promise/catch-or-return */ + +/** + * Local static server serving index.html running the compiled version + * of `@asd14/fetch-browser` + */ +const httpServer = require("http-server").createServer({ + root: "test.app/dist", + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Credentials": "true", + }, +}) + +httpServer.listen(50123) + +/** + * Run all tests via local test runner + */ +const createTestCafe = require("testcafe") + +createTestCafe("localhost", 1337, 1338).then(async testcafe => { + const runner = testcafe.createRunner() + + await runner + .src(["test.scenarios/GET.test.js"]) + .browsers(["firefox:/usr/bin/firefox-developer-edition"]) + .run() + + testcafe.close() + httpServer.close() +})