Skip to content

Commit

Permalink
chore: add request.cy.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Feb 1, 2024
1 parent 60d2571 commit 335fe7e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 93 deletions.
93 changes: 0 additions & 93 deletions projects/cdk/services/test/static-request.service.spec.ts

This file was deleted.

85 changes: 85 additions & 0 deletions projects/demo-cypress/src/tests/static-request.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {TestBed} from '@angular/core/testing';
import {TuiStaticRequestService} from '@taiga-ui/cdk';

describe('Tabs', () => {
let service: TuiStaticRequestService;
const baseUrl = 'http://localhost:8080';
const response = {
status: 200,
contentType: 'text/plain',
responseText: 'awesome response',
};

beforeEach(() => {
cy.intercept('GET', '**test*', response)
.as('request')
.then(() => {
TestBed.configureTestingModule({
providers: [TuiStaticRequestService],
});

service = TestBed.inject(TuiStaticRequestService);
});
});

it('returns the same observable on second request for the same URL', () => {
const result = service.request(`${baseUrl}/test`);
const second = service.request(`${baseUrl}/test`);

expect(result).to.eql(second);
});

it('returns the different observable on second request for different URL', () => {
const result = service.request(`${baseUrl}/test1`);
const second = service.request(`${baseUrl}/test2`);

expect(result).not.to.eql(second);
});

it('the request is made to the url and completes', () => {
let result = '';
let completed = false;

service.request(`${baseUrl}/test`).subscribe({
next: response => {
result = response;
},
complete: () => {
completed = true;
},
});

cy.get('@request.all')
.should('have.length', 1)
.then(() => {
expect(JSON.parse(result)).to.deep.equal(response);
void expect(completed).to.be.true;
});
});

it('the request is only made once, same result is returned on the subsequent calls', () => {
let result1 = '';
let result2 = '';
let completed = false;

service.request(`${baseUrl}/test`).subscribe(response => {
result1 = response;
});

service.request(`${baseUrl}/test`).subscribe({
next: response => {
result2 = response;
},
complete: () => {
completed = true;
},
});

cy.get('@request.all')
.should('have.length', 1)
.then(() => {
expect(JSON.parse(result1)).to.deep.equal(JSON.parse(result2));
void expect(completed).to.be.true;
});
});
});

0 comments on commit 335fe7e

Please sign in to comment.