Skip to content

Commit

Permalink
chore: convert fake into stub
Browse files Browse the repository at this point in the history
Since we are only using in a reduced part of the codebase, I would
rather keep it as a stub than a more comples fake.
  • Loading branch information
DanielMoraDC committed Feb 15, 2025
1 parent 85fb5a9 commit 3ab7960
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/lib/clients/artists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { beforeEach, describe, it, expect, vi } from 'vitest';
import { Artist } from '@/lib/artists';
import { ArtistsHTTPClient } from './artists';
import { FakeHttpClient, HttpResponse, Method } from './http';
import { FakeAuthHeaderBuilder } from './auth';
import { AuthHeaderBuilderStub } from './auth';

describe('ArtistsHTTPClient', () => {
let url: string;
Expand All @@ -29,7 +29,7 @@ describe('ArtistsHTTPClient', () => {

it('should call the client with the correct parameters', async () => {
const headers = { something: 'value' };
const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder(headers);
const httpAuthHeaderBuilder = new AuthHeaderBuilderStub(headers);
const client = new ArtistsHTTPClient(
url,
httpClient,
Expand All @@ -48,7 +48,7 @@ describe('ArtistsHTTPClient', () => {
});

it('should return the list of artists returned by the HTTP client', async () => {
const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder();
const httpAuthHeaderBuilder = new AuthHeaderBuilderStub();
const client = new ArtistsHTTPClient(
url,
httpClient,
Expand All @@ -67,7 +67,7 @@ describe('ArtistsHTTPClient', () => {
it('should throw an error if the HTTP client fails', async () => {
const errorMessage = 'Request failed';
httpClient.setSendErrorMessage(errorMessage);
const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder();
const httpAuthHeaderBuilder = new AuthHeaderBuilderStub();
const client = new ArtistsHTTPClient(
url,
httpClient,
Expand All @@ -80,7 +80,7 @@ describe('ArtistsHTTPClient', () => {
});

it('should throw an error if the header builder fails', async () => {
const httpAuthHeaderBuilder = new FakeAuthHeaderBuilder();
const httpAuthHeaderBuilder = new AuthHeaderBuilderStub();
vi.spyOn(httpAuthHeaderBuilder, 'buildHeader').mockImplementation(() => {
throw new Error('test Error');
});
Expand Down
8 changes: 5 additions & 3 deletions src/lib/clients/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, it, expect, vi } from 'vitest';
import {
FakeAuthClient,
AuthClientStub,
GCPAuthClient,
BaseHTTPAuthHeaderBuilder,
} from './auth';
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('AuthClient', () => {
function createAuthClient() {
const authHeader = 'X-Serverless-Authorization';
const authToken = 'test-token';
return new FakeAuthClient(authToken, authHeader);
return new AuthClientStub(authToken, authHeader);
}

it('should call getToken and getHeaderName on authClient', async () => {
Expand Down Expand Up @@ -97,7 +97,9 @@ describe('AuthClient', () => {
it('should throw an error if the auth client fails', async () => {
const errorMessage = 'Auth request failed';
const authClient = createAuthClient();
authClient.setGetTokenErrorMessage(errorMessage);
vi.spyOn(authClient, 'getToken').mockImplementation(() => {
throw new Error(errorMessage);
});
const client = new BaseHTTPAuthHeaderBuilder(authClient);

const token = 'app-token';
Expand Down
12 changes: 2 additions & 10 deletions src/lib/clients/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export class GCPAuthClient implements AuthClient {
}
}

export class FakeAuthClient {
export class AuthClientStub implements AuthClient {
private header: string;
private token: string;
private getTokenErrorMessage: string | undefined = undefined;

constructor(token: string, header: string) {
this.token = token;
Expand All @@ -50,14 +49,7 @@ export class FakeAuthClient {
this.token = token;
}

setGetTokenErrorMessage(message: string) {
this.getTokenErrorMessage = message;
}

async getToken(): Promise<string> {
if (this.getTokenErrorMessage !== undefined) {
throw new Error(this.getTokenErrorMessage);
}
return this.token;
}

Expand Down Expand Up @@ -91,7 +83,7 @@ export class BaseHTTPAuthHeaderBuilder implements HTTPAuthHeaderBuilder {
}
}

export class FakeAuthHeaderBuilder implements HTTPAuthHeaderBuilder {
export class AuthHeaderBuilderStub implements HTTPAuthHeaderBuilder {
private headers: Record<string, string>;

constructor(headers: Record<string, string> = {}) {
Expand Down

0 comments on commit 3ab7960

Please sign in to comment.