import {inject, TestBed} from '@angular/core/testing'; import {CrdsContentBlockService, GET_COPY} from './crds-content-block.service'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {ApolloTestingController, ApolloTestingModule} from 'apollo-angular/testing'; import {ContentBlock} from './model/content-block'; describe('CrdsContentBlockService', () => { beforeEach(() => { TestBed.configureTestingModule({ imports: [ HttpClientTestingModule, [ApolloTestingModule.withClients(['contentBlockClient'])] ], providers: [ { provide: 'Config', useValue: {endpoint: 'https://api-int.crossroads.net/graphql-gateway', categories: ['common']}, }, ] }); }); it('should be created', () => { inject([CrdsContentBlockService], (service: CrdsContentBlockService) => { expect(service).toBeTruthy(); }); }); it('should get all results back', () => { const service = TestBed.inject(CrdsContentBlockService); const apolloTestController = TestBed.inject(ApolloTestingController); service.getContentBlocks().subscribe((contentBlocks) => { expect(contentBlocks.length).toEqual(mockListOfContentBlocks.length); expect(contentBlocks[0].content).toEqual(mockListOfContentBlocks[0].content); expect(contentBlocks[0].title).toEqual(mockListOfContentBlocks[0].title); expect(contentBlocks[1].content).toEqual(mockListOfContentBlocks[1].content); expect(contentBlocks[1].title).toEqual(mockListOfContentBlocks[1].title); }); const testQuery = apolloTestController.expectOne(GET_COPY); expect(testQuery.operation.variables.category).toEqual(['common']); testQuery.flush({ data: { contentBlocks: mockListOfContentBlocks} }); apolloTestController.verify(); }); it('should only return content block with matching title', () => { const service = TestBed.inject(CrdsContentBlockService); const apolloTestController = TestBed.inject(ApolloTestingController); service.getContent(mockListOfContentBlocks[0].title).subscribe((contentBlock) => { expect(contentBlock.title).toEqual(mockListOfContentBlocks[0].title); expect(contentBlock.content).toEqual(mockListOfContentBlocks[0].content); }); const testQuery = apolloTestController.expectOne(GET_COPY); expect(testQuery.operation.variables.category).toEqual(['common']); testQuery.flush({ data: { contentBlocks: mockListOfContentBlocks} }); apolloTestController.verify(); }); it('should not return content block when none match', () => { const service = TestBed.inject(CrdsContentBlockService); const apolloTestController = TestBed.inject(ApolloTestingController); service.getContent('no title').subscribe((contentBlock) => { expect(contentBlock).toBeUndefined(); }); const testQuery = apolloTestController.expectOne(GET_COPY); expect(testQuery.operation.variables.category).toEqual(['common']); testQuery.flush({ data: { contentBlocks: mockListOfContentBlocks} }); apolloTestController.verify(); }); it('should only return content block with matching id', (done) => { const service = TestBed.inject(CrdsContentBlockService); const apolloTestController = TestBed.inject(ApolloTestingController); service.getContentById(mockListOfContentBlocks[0].legacy_id).subscribe((contentBlock) => { expect(contentBlock.title).toEqual(mockListOfContentBlocks[0].title); expect(contentBlock.content).toEqual(mockListOfContentBlocks[0].content); done(); }); const testQuery = apolloTestController.expectOne(GET_COPY); expect(testQuery.operation.variables.category).toEqual(['common']); testQuery.flush({ data: { contentBlocks: mockListOfContentBlocks} }); apolloTestController.verify(); }); it('should not return content block when no id match', () => { const service = TestBed.inject(CrdsContentBlockService); const apolloTestController = TestBed.inject(ApolloTestingController); service.getContent(124).subscribe((contentBlock) => { expect(contentBlock).toBeUndefined(); }); const testQuery = apolloTestController.expectOne(GET_COPY); expect(testQuery.operation.variables.category).toEqual(['common']); testQuery.flush({ data: { contentBlocks: mockListOfContentBlocks} }); apolloTestController.verify(); }); }); const mockListOfContentBlocks: ContentBlock[] = [{ content: 'Hello', legacy_id: 123, title: 'first title', category: 'common', type: '', className: '', id: 0 }, { content: 'world', legacy_id: 124, title: 'Second title', type: '', category: 'common', className: '', id: 0 }];