Skip to content

Commit 44830c5

Browse files
committed
jester exercises are created according to what has been learned in classes on testing
0 parents  commit 44830c5

19 files changed

+6061
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

babel.config.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
/*"@babel/preset-typescript",*/
5+
],
6+
};

docs/README.de.md

Whitespace-only changes.

docs/README.es.md

Whitespace-only changes.

docs/README.md

Whitespace-only changes.

etc/1.KCWB_tdd-pro.pdf

727 KB
Binary file not shown.

examples/Carrito.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class Carrito {
2+
items = 0;
3+
4+
getTotalItems() {
5+
return this.items;
6+
}
7+
8+
getTotalCheckout() {
9+
return 10;
10+
}
11+
12+
addItem(item) {
13+
if (!item.price || !item.name)
14+
throw new Error("Item must have price and name");
15+
16+
if (typeof item !== "object") {
17+
throw new Error("Item must be an object");
18+
}
19+
this.items++;
20+
}
21+
}

examples/Carrito.spect.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Carrito } from "./Carrito";
2+
3+
describe("Testing de la clase Carrito", () => {
4+
const sushiItem = {
5+
price: 10,
6+
name: "sushiItem",
7+
};
8+
9+
let carrito;
10+
beforeEach(() => {
11+
carrito = new Carrito();
12+
});
13+
14+
describe("Testing de getTotalItems y addItem", () => {
15+
it("Carrito debe tener una fución llamada getTotalItems que devuelva 0 a la inicialización del carrito", () => {
16+
// const carrito = new Carrito();
17+
expect(carrito.getTotalItems()).toEqual(0);
18+
});
19+
20+
it("Carrito.getTotalItems debe devolver 1 despues de añadir un elemento al carrito.", () => {
21+
// const carrito = new Carrito();
22+
// carrito.addItem({ price: 1, name: 'sushiItem' });
23+
carrito.addItem(sushiItem);
24+
expect(carrito.getTotalItems()).toEqual(1);
25+
});
26+
27+
it("Carrito.getTotalItems debe devolver 2 despues de añadir 1 elemento al carrito dos veces.", () => {
28+
// carrito.addItem({ price: 1, name: 'sushiItem' });
29+
// carrito.addItem({ price: 1, name: 'sushiItem' });
30+
carrito.addItem(sushiItem);
31+
carrito.addItem(sushiItem);
32+
expect(carrito.getTotalItems()).toEqual(2);
33+
});
34+
35+
describe("Gestión de errores", () => {
36+
it("Carrito.addItem debe devolver un error si se añade un item sin precio.", () => {
37+
expect(() => carrito.addItem({ name: "sushiItem" })).toThrow();
38+
});
39+
40+
it("Carrito.addItem debe devolver un error si se añade un item sin nombre.", () => {
41+
expect(() => carrito.addItem({ price: 1 })).toThrow();
42+
});
43+
44+
it("Carrito.addItem debe devolver un error diciendo 'Item must have price and name' si el item no contiene nombre o precio.", () => {
45+
expect(() => carrito.addItem({ price: 1 })).toThrow(
46+
"Item must have price and name"
47+
);
48+
expect(() => carrito.addItem({ name: "sushiItem" })).toThrow(
49+
"Item must have price and name"
50+
);
51+
});
52+
53+
it("Carrito.addItem debe devolver un error diciendo 'Item must be an object' si el item no es un objeto.", () => {
54+
expect(() => carrito.addItem("sushiItem")).toThrow(
55+
"Item must be an object"
56+
);
57+
});
58+
});
59+
});
60+
61+
describe("Testeando getTotalCheckout", () => {
62+
it.todo(
63+
"Carrito.getTotatCheckout debe devolver 10 despues de añadir 1 sushiItem",
64+
() => {
65+
carrito.addItem(sushiItem);
66+
expect(carrito.getTotalCheckout()).toEqual(1);
67+
}
68+
);
69+
});
70+
});

examples/Users.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import axios from "axios";
2+
3+
const BASE_URL = "https://jsonplaceholder.typicode.com";
4+
5+
export class Users {
6+
static getAll() {
7+
return axios.get(`${BASE_URL}/users`).then((res) => res.data);
8+
}
9+
}

examples/Users.spec.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Users } from "./Users";
2+
import axios from "axios";
3+
jest.mock("axios");
4+
5+
describe("Testing de la clase User", () => {
6+
// Testing con datos reales
7+
describe.skip("Utilizando api real", () => {
8+
// Hooks
9+
let data;
10+
beforeAll(async () => {
11+
// Se dispara una unica vez antes de TODOS LOS TEST
12+
data = await Users.getAll();
13+
});
14+
15+
beforeEach(() => {
16+
// Se dispara antes de CADA TEST
17+
// console.log(data);
18+
});
19+
20+
afterAll(() => {
21+
data = undefined;
22+
});
23+
24+
it("Debe devolver un array con 10 usuarios cuando llamamos a getAll", async () => {
25+
expect.assertions(2);
26+
// const data = await Users.getAll();
27+
expect(data).toBeArray();
28+
expect(data).toHaveLength(10);
29+
});
30+
31+
it("Debe devolver un usuario con username 'Samantha' en la posición 2", async () => {
32+
expect.assertions(3);
33+
// const data = await Users.getAll();
34+
expect(data[2]).toHaveProperty("username");
35+
expect(data[2]).toContainEntry(["username", "Samantha"]);
36+
expect(data[2].username).toMatch(/Samantha/);
37+
});
38+
});
39+
40+
// Mock
41+
describe("Utilizando mocks", () => {
42+
let data;
43+
beforeAll(async () => {
44+
const mockUsers = Array.from({ length: 10 });
45+
mockUsers[2] = {
46+
username: "Samantha",
47+
};
48+
axios.get.mockResolvedValue({ data: mockUsers });
49+
data = await Users.getAll();
50+
console.log(data);
51+
});
52+
53+
it("Debe devolver un array con 10 usuarios cuando llamamos a getAll", async () => {
54+
expect.assertions(2);
55+
// const data = await Users.getAll();
56+
expect(data).toBeArray();
57+
expect(data).toHaveLength(10);
58+
});
59+
60+
it("Debe devolver un usuario con username 'Samantha' en la posición 2", async () => {
61+
expect.assertions(3);
62+
// const data = await Users.getAll();
63+
expect(data[2]).toHaveProperty("username");
64+
expect(data[2]).toContainEntry(["username", "Samantha"]);
65+
expect(data[2].username).toMatch(/Samantha/);
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)