Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions frontend/src/services/jobsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ function readMessage(payload: unknown): string | undefined {
}

export async function fetchJobFiles(): Promise<JobFile[]> {
const response = await fetch(buildApiUrl("/api/jobs/files"));
const response = await fetch(buildApiUrl("/api/jobs/files"), {
credentials: "include",
});
const payload = (await readPayload(response)) as { files?: unknown } & Record<
string,
unknown
Expand Down Expand Up @@ -86,7 +88,9 @@ export async function fetchJobFiles(): Promise<JobFile[]> {

export async function fetchJobsByFile(fileName: string): Promise<JobsResponse> {
const suffix = fileName ? `?file=${encodeURIComponent(fileName)}` : "";
const response = await fetch(buildApiUrl(`/api/jobs${suffix}`));
const response = await fetch(buildApiUrl(`/api/jobs${suffix}`), {
credentials: "include",
});
const payload = (await readPayload(response)) as Record<string, unknown>;

if (!response.ok) {
Expand All @@ -106,7 +110,9 @@ export async function fetchJobsByFile(fileName: string): Promise<JobsResponse> {
}

export async function fetchKeywords(): Promise<string[]> {
const response = await fetch(buildApiUrl("/api/keywords"));
const response = await fetch(buildApiUrl("/api/keywords"), {
credentials: "include",
});
const payload = (await readPayload(response)) as {
keywords?: unknown;
} & Record<string, unknown>;
Expand All @@ -125,6 +131,7 @@ export async function saveKeywords(keywords: string[]): Promise<void> {
"Content-Type": "application/json",
},
body: JSON.stringify({ keywords }),
credentials: "include",
});
const payload = (await readPayload(response)) as Record<string, unknown>;

Expand All @@ -136,6 +143,7 @@ export async function saveKeywords(keywords: string[]): Promise<void> {
export async function runScraperRequest(): Promise<void> {
const response = await fetch(buildApiUrl("/api/jobs/search"), {
method: "POST",
credentials: "include",
});
const payload = (await readPayload(response)) as Record<string, unknown>;

Expand Down
9 changes: 5 additions & 4 deletions frontend/tests/unit/utils/jobsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("jobsService", () => {
vi.stubGlobal("fetch", fetchMock);

await fetchJobFiles();
expect(fetchMock).toHaveBeenCalledWith("https://jobsglobalscraper.ddns.net/api/jobs/files");
expect(fetchMock).toHaveBeenCalledWith("https://jobsglobalscraper.ddns.net/api/jobs/files", { credentials: "include" });
});

it("filtra entradas invalidas ao listar arquivos", async () => {
Expand Down Expand Up @@ -68,7 +68,7 @@ describe("jobsService", () => {
const response = await fetchJobsByFile("vagas.xlsx");
expect(response.total).toBe(1);
expect(response.file).toBe("vagas.xlsx");
expect(fetchMock).toHaveBeenCalledWith("/api/jobs?file=vagas.xlsx");
expect(fetchMock).toHaveBeenCalledWith("/api/jobs?file=vagas.xlsx", { credentials: "include" });
});

it("usa endpoint sem sufixo quando fileName vazio", async () => {
Expand All @@ -81,7 +81,7 @@ describe("jobsService", () => {
);

await fetchJobsByFile("");
expect(fetch).toHaveBeenCalledWith("/api/jobs");
expect(fetch).toHaveBeenCalledWith("/api/jobs", { credentials: "include" });
});

it("normaliza payload invalido ao buscar jobs", async () => {
Expand All @@ -92,7 +92,7 @@ describe("jobsService", () => {

const response = await fetchJobsByFile("vagas com espaco.xlsx");
expect(response).toEqual({ jobs: [], file: "", modifiedAt: null, total: 0 });
expect(fetch).toHaveBeenCalledWith("/api/jobs?file=vagas%20com%20espaco.xlsx");
expect(fetch).toHaveBeenCalledWith("/api/jobs?file=vagas%20com%20espaco.xlsx", { credentials: "include" });
});

it("lanca erro com mensagem da API ao buscar jobs", async () => {
Expand Down Expand Up @@ -173,6 +173,7 @@ describe("jobsService", () => {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ keywords: ["node", "vitest"] }),
credentials: "include",
});
});

Expand Down
Loading