Skip to content

Commit

Permalink
Add tests for useDeputadoDetails
Browse files Browse the repository at this point in the history
This is part of #98
  • Loading branch information
kimuraz committed Oct 25, 2023
1 parent c4e34ab commit 5851860
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
55 changes: 55 additions & 0 deletions src/composables/useDeputadoDetails.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it, vi } from "vitest";

import useDeputadoDetails from "./useDeputadoDetails";

vi.mock("@/utils/fetchCamaraAPI", () => {
return {
default: (url: string) => {
if (url === "/deputados/1") {
return { dados: { id: 1 } };
}
if (url === "/deputados/3") {
throw {};
}
throw new Error("Not found");
},
};
});
describe("useDeputadoDetails", () => {
it("should return default values", () => {
const { deputado, loading, error, getDeputadoDetails } = useDeputadoDetails();
expect(deputado.value).toBeNull();
expect(loading.value).toBe(false);
expect(error.value).toBeNull();
expect(getDeputadoDetails).toBeInstanceOf(Function);
});

it("should fetch deputado details", async () => {
const { deputado, loading, error, getDeputadoDetails } = useDeputadoDetails();

await getDeputadoDetails(1);
expect(deputado.value).toEqual({ id: 1 });
expect(loading.value).toBe(false);
expect(error.value).toBeNull();
});

it("should handle error", async () => {
const { deputado, loading, error, getDeputadoDetails } = useDeputadoDetails();

await getDeputadoDetails(2);
expect(deputado.value).toBeNull();
expect(loading.value).toBe(false);
expect(error.value).toBe("Not found");
});

it("should throw unknown error", async () => {
const err = console.err;
console.err = vi.fn();
const { deputado, loading, error, getDeputadoDetails } = useDeputadoDetails();

await getDeputadoDetails(3);
expect(deputado.value).toBeNull();
expect(loading.value).toBe(false);
expect(error.value).toBe("An unknown error occurred.");
});
});
12 changes: 6 additions & 6 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, '/src'),
},
alias: [
{ find: "@", replacement: path.resolve(__dirname, "src") },
],
},
});
5 changes: 4 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="vitest" />
import { defineConfig } from "vite";
import { defineConfig } from "vitest/config";
import vue from "@vitejs/plugin-vue";

export default defineConfig({
Expand All @@ -11,6 +11,9 @@ export default defineConfig({
reporter: ["text", "html"],
extension: [".vue", ".ts", ".js"],
},
alias: {
"@/": new URL("./src/", import.meta.url).pathname,
},
},
plugins: [
vue(),
Expand Down

0 comments on commit 5851860

Please sign in to comment.