From fff70e3712fc98020f44a62c05778205bed601d7 Mon Sep 17 00:00:00 2001 From: splincode Date: Thu, 1 Feb 2024 19:17:04 +0300 Subject: [PATCH] chore: add `request.cy.ts` --- .../test/static-request.service.spec.ts | 93 ------------------- .../src/tests/static-request.cy.ts | 72 ++++++++++++++ 2 files changed, 72 insertions(+), 93 deletions(-) delete mode 100644 projects/cdk/services/test/static-request.service.spec.ts create mode 100644 projects/demo-cypress/src/tests/static-request.cy.ts diff --git a/projects/cdk/services/test/static-request.service.spec.ts b/projects/cdk/services/test/static-request.service.spec.ts deleted file mode 100644 index 9785deb1f825..000000000000 --- a/projects/cdk/services/test/static-request.service.spec.ts +++ /dev/null @@ -1,93 +0,0 @@ -import {TuiStaticRequestService} from '@taiga-ui/cdk'; - -const RESPONSE = { - status: 200, - contentType: 'text/plain', - responseText: 'awesome response', -}; - -// TODO: need replace jasmine.Ajax -// TODO: move to cypress component testing -xdescribe('TuiStaticRequest service', () => { - let service: TuiStaticRequestService; - - beforeEach(() => { - service = new TuiStaticRequestService({} as any, {} as any); - // @ts-ignore - // eslint-disable-next-line jest/no-jasmine-globals - jasmine.Ajax.install(); - }); - - afterEach(() => { - // @ts-ignore - // eslint-disable-next-line jest/no-jasmine-globals - jasmine.Ajax.uninstall(); - }); - - it('returns the same observable on second request for the same URL', () => { - const result = service.request('test'); - const second = service.request('test'); - - expect(result).toBe(second); - }); - - it('returns the different observable on second request for different URL', () => { - const result = service.request('test1'); - const second = service.request('test2'); - - expect(result).not.toBe(second); - }); - - it('the request is made to the url and completes', () => { - let result = ''; - let completed = false; - - service.request('test').subscribe({ - next: response => { - result = response; - }, - complete: () => { - completed = true; - }, - }); - - // @ts-ignore - // eslint-disable-next-line jest/no-jasmine-globals - jasmine.Ajax.requests.mostRecent().respondWith(RESPONSE); - - // @ts-ignore - // eslint-disable-next-line jest/no-jasmine-globals - expect(jasmine.Ajax.requests.mostRecent().url).toBe('test'); - expect(result).toBe('awesome response'); - expect(completed).toBe(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('test').subscribe(response => { - result1 = response; - }); - - // @ts-ignore - // eslint-disable-next-line jest/no-jasmine-globals - jasmine.Ajax.requests.mostRecent().respondWith(RESPONSE); - - service.request('test').subscribe({ - next: response => { - result2 = response; - }, - complete: () => { - completed = true; - }, - }); - - // @ts-ignore - // eslint-disable-next-line jest/no-jasmine-globals - expect(jasmine.Ajax.requests.count()).toBe(1); - expect(result2).toBe(result1); - expect(completed).toBe(true); - }); -}); diff --git a/projects/demo-cypress/src/tests/static-request.cy.ts b/projects/demo-cypress/src/tests/static-request.cy.ts new file mode 100644 index 000000000000..7aa6b661c0ed --- /dev/null +++ b/projects/demo-cypress/src/tests/static-request.cy.ts @@ -0,0 +1,72 @@ +import {TestBed} from '@angular/core/testing'; +import {TuiStaticRequestService} from '@taiga-ui/cdk'; + +describe('TuiStaticRequestService', () => { + 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); + cy.get('@request.all').should('have.length', 0); + }); + + 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); + cy.get('@request.all').should('have.length', 0); + }); + + it('the request is made to the url and completes', () => { + service.request(`${baseUrl}/test`).subscribe(); + + cy.get('@request').its('response.body').should('deep.equal', response); + }); + + 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(result1).to.equal(result2); + void expect(completed).to.be.true; + }); + }); +});