Angular Jasmine + Contentful: How to Test a Contentful call?

Published

I’m using Contentful to load text into my Angular Singel Page Application. It works fine but now I want to test it. The test should actually call Contentful and load the text of myPage from my space. Then I want to assert that an expected value (the title field of myPage) is not null.
This way I want to make sure that calling Contentful works from my Frontend / SPA.

The code where I call Contentful is put inside a service called contentful.service.ts. This is the test that I tried for this service:

contentful.service.spec

describe('ContentfulService', () => {
  let service: ContentfulService;

  //Arrange
  beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(ContentfulService);
  });

it('should get myPage for the default language en-US', (done) => {   
    //Act
    service.getMyPage("en-US").then(myPage => {
      //Assert
      expect(myPage.fields.title.length).toBeGreaterThan(0);
      done();
    });
 } 

This is my contentful.service.ts:

export class ContentfulService {
  private clientConsentPage = createClient({
    space: environment.contentfulConsent.spaceId,
    accessToken: environment.contentfulConsent.access_token,
    environment: environment.contentfulConsent.environment
  });

constructor() { }

public getMyPage(locale: string): Promise<Entry<any>> {
    return this.clientConsentPage.getEntries(Object.assign({
      content_type: 'myPage'
    }, { 'sys.id': environment.contentfulConsent.contentTypeIds.myPage, locale: locale }))
      .then(res => res.items[0]);
}

As I said this code works fine, only the test fails with either one of the following error:

TypeError: clientConsentPage.getEntries is not a function

or

Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

(I don’t know why it’s not always the same error…)

Source: Angular Questions

Published
Categorized as angular, contentful, jasmine Tagged , ,

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq