From 8269e05347cedd71651a6eb0cbd9571704c61115 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 11 Oct 2025 14:37:50 +0200 Subject: [PATCH] test: Formulated more expections for the pact --- .../chatroom/chatroom.service.pact.spec.ts | 95 ++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/src/app/chatroom/chatroom.service.pact.spec.ts b/src/app/chatroom/chatroom.service.pact.spec.ts index 38838f18..44a59481 100644 --- a/src/app/chatroom/chatroom.service.pact.spec.ts +++ b/src/app/chatroom/chatroom.service.pact.spec.ts @@ -1,6 +1,6 @@ import { Matchers, Pact, SpecificationVersion } from '@pact-foundation/pact'; import { TestBed } from '@angular/core/testing'; -import { provideHttpClient } from '@angular/common/http'; +import { HttpErrorResponse, provideHttpClient } from '@angular/common/http'; import { lastValueFrom } from 'rxjs'; import { ChatroomService } from './chatroom.service'; import { Chatroom } from './chatroom.model'; @@ -51,4 +51,97 @@ describe('Pact between the ChatroomService and the backend', () => { expect(chatrooms).toContainEqual(EXAMPLE_CHATROOM); }); }); + + it('a request for /list returns an HTTP 200 and an empty list, if no chatrooms are known', async () => { + + await provider + .addInteraction() + .given('no chatrooms are known') + .uponReceiving('a request for /list') + .withRequest('GET', '/list') + .willRespondWith(200, (builder) => { + builder.headers({ 'Content-Type': 'application/json' }); + builder.jsonBody([]); + }) + .executeTest(async (mockserver) => { + await TestBed.configureTestingModule({ + providers: [ + ChatroomService, + {provide: APP_PROPS, useValue: {backendUri: mockserver.url + '/'}}, + provideHttpClient(), + ] + }); + + const service = TestBed.inject(ChatroomService); + const chatrooms = await lastValueFrom(service.getChatrooms()); + + expect(chatrooms).toHaveLength(0); + }); + }); + + it('a request for /1 with header "X-Shard: 7" returns an HTTP 200 and the chatroom, if the chatroom with id "1" exists in shard 7', async () => { + + await provider + .addInteraction() + .given('chatroom with ID "1" exists in shard "7"') + .uponReceiving('a request for /1') + .withRequest('GET', '/1', (builder) => { + builder.headers({ 'X-Shard': 7 }) + }) + .willRespondWith(200, (builder) => { + builder.headers({ 'Content-Type': 'application/json' }); + builder.jsonBody(EXAMPLE_CHATROOM); + }) + .executeTest(async (mockserver) => { + await TestBed.configureTestingModule({ + providers: [ + ChatroomService, + {provide: APP_PROPS, useValue: {backendUri: mockserver.url + '/'}}, + provideHttpClient(), + ] + }); + + const service = TestBed.inject(ChatroomService); + const chatroom = await lastValueFrom(service.getChatroom("7", "1")); + + expect(chatroom).toEqual(EXAMPLE_CHATROOM); + }); + }); + + it('a request for /1 with header "X-Shard: 6" returns HTTP 404, if the chatroom with id "1" exists in shard 7', async () => { + + await provider + .addInteraction() + .given('chatroom with ID "1" exists in shard "7"') + .uponReceiving('a request for /1') + .withRequest('GET', '/1', (builder) => { + builder.headers({ 'X-Shard': 6 }) + }) + .willRespondWith(404) + .executeTest(async (mockserver) => { + await TestBed.configureTestingModule({ + providers: [ + ChatroomService, + {provide: APP_PROPS, useValue: {backendUri: mockserver.url + '/'}}, + provideHttpClient(), + ] + }); + + const service = TestBed.inject(ChatroomService); + + return new Promise((resolve, reject) => { + + service + .getChatroom("6", "1") + .subscribe({ + next: (chatrooms) => { + reject('a status other than 200 should have rejected the observable') + }, + error: (error) => { + resolve(); + }, + }); + }); + }); + }); }); -- 2.39.5