Created
April 12, 2020 10:43
-
-
Save voskresla/9307bd7d67b92866bb2f2ee1a473fd81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mock from "xhr-mock"; | |
import fetch from "../src/fetch"; | |
describe("fetch", () => { | |
beforeEach(() => { | |
mock.setup(); | |
// mock.error(({ req, err }) => console.log(req.url(), err)); | |
}); | |
afterEach(() => { | |
mock.teardown(); | |
}); | |
// TODO: посомотреть почему на первых двух тестах валиться xhr-moch | |
it("Возвращает 'Incorrect url' если нет параметра URL", () => { | |
expect.assertions(2); | |
return fetch().catch(e => { | |
expect(e).toBeInstanceOf(Error); | |
expect(e.message).toBe("Incorrect url"); | |
}); | |
}); | |
it("Возвращает 'Incorrect url' если параметр URL пустая строка", () => { | |
expect.assertions(2); | |
return fetch("").catch(e => { | |
expect(e).toBeInstanceOf(Error); | |
expect(e.message).toBe("Incorrect url"); | |
}); | |
}); | |
it("Возвращает 'Incorrect url' если параметр URL не строка", () => { | |
// кажеться так будет праивльнее при первом запуске. | |
expect.assertions(2); | |
return fetch({}).catch(e => { | |
expect(e).toBeInstanceOf(Error); | |
expect(e.message).toBe("Incorrect url"); | |
}); | |
}); | |
it("Использует GET по-умолчанию", () => { | |
// кажеться так будет праивльнее при первом запуске. | |
expect.assertions(1); | |
mock.get("https://jsonplaceholder.typicode.com/todos/1", (req, res) => { | |
expect(req.method()).toBe("GET"); | |
return res.status(200); | |
}); | |
return fetch("https://jsonplaceholder.typicode.com/todos/1"); | |
}); | |
it("Возвращает 'Invalid Method' если он не в allowedMethods", () => { | |
// кажеться так будет праивльнее при первом запуске. | |
expect.assertions(2); | |
return fetch("https://jsonplaceholder.typicode.com/todos/1/", { | |
method: "PUSH" | |
}).catch(e => { | |
expect(e).toBeInstanceOf(Error); | |
expect(e.message).toBe("Invalid method"); | |
}); | |
}); | |
// TODO: как запустить кучу fetch с разными методами и проверить все в одном тесте? | |
it("Правильно устанавливает методы в request [POST]", () => { | |
// кажеться так будет праивльнее при первом запуске. | |
expect.assertions(1); | |
mock.post("https://jsonplaceholder.typicode.com/todos/1", (req, res) => { | |
expect(req.method()).toBe("POST"); | |
return res.status(200); | |
}); | |
return fetch("https://jsonplaceholder.typicode.com/todos/1", { | |
method: "POST" | |
}); | |
}); | |
it("Правильно устанавливает body = null при GET и body = new FormData()", () => { | |
expect.assertions(2); | |
mock.get("https://jsonplaceholder.typicode.com/todos/1", (req, res) => { | |
expect(req.method()).toBe("GET"); | |
expect(req.body()).toBe(null); | |
return res.status(200); | |
}); | |
return fetch("https://jsonplaceholder.typicode.com/todos/1", { | |
method: "GET", | |
body: new FormData() | |
}); | |
}); | |
it("Правильно не отправляет запрос при POST и body != new FormData() || string", () => { | |
expect.assertions(2); | |
mock.post("https://jsonplaceholder.typicode.com/todos/1", (req, res) => { | |
expect(req.method()).toBe("POST"); | |
expect(req.body()).toBe(null); | |
return res.status(200); | |
}); | |
return fetch("https://jsonplaceholder.typicode.com/todos/1", { | |
method: "POST", | |
body: {} | |
}).catch(e => { | |
expect(e).toBeInstanceOf(Error); | |
expect(e.message).toBe("Something went wrong: invalidBody"); | |
}); | |
}); | |
it("Возвращает ошибку 'Invalid body' при POST и body != new FormData() || string", () => { | |
expect.assertions(2); | |
return fetch("https://jsonplaceholder.typicode.com/todos/1", { | |
method: "POST", | |
body: {} | |
}).catch(e => { | |
expect(e).toBeInstanceOf(Error); | |
expect(e.message).toBe("Something went wrong: invalidBody"); | |
}); | |
}); | |
it("Правильно возвращает req.method === 'GET' в response", () => { | |
expect.assertions(1); | |
mock.get("/test", (req, res) => { | |
return res; | |
}); | |
return fetch("/test", { | |
method: "GET" | |
}).then(response => expect(response.method).toBe("GET")); | |
}); | |
it.only("Правильно возвращает промис response.text()", () => { | |
expect.assertions(1); | |
mock.delete("/test", (req, res) => { | |
return res.body("test payload string"); | |
}); | |
return fetch("/test", { | |
method: "DELETE", | |
body: "test payload string" | |
}) | |
.then(response => response.text()) | |
.then(text => expect(text).toBe("test payload string")); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment