npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crds-cypress-contentful

v2.0.2

Published

Cypress methods to access Contentful

Downloads

253

Readme

Contentful Tools

This package provides a couple tools to make accessing Contentful from Cypress tests and using results easier.

Tools include:

  • Contentful Query Plugin requests information from Contentful
  • Query Builders construct correctly formatted queries by configuration
  • Text Normalizer strip markdown, special characters, extra whitespace etc. from given text to make comparing text from content and browser easier

Install

Install with

npm install crds-cypress-contentful --save-dev

Required Environment Variables

CONTENTFUL_SPACE_ID
CONTENTFUL_ACCESS_TOKEN
CONTENTFUL_ENV

Contentful Query Plugin

Setup

This tool works best when used as a cy.task(). To configure this, add the following to the cypress/plugins/index.js file:

const contentfulPlugin = require(`crds-cypress-contentful`);

function addContentfulTasks(on, config){  
  const spaceId = config.env.SPACE_ID;
  const environment = config.env.ENVIRONMENT;
  const accessToken = config.env.ACCESS_TOKEN;
  
  const qp = contentfulPlugin.ContentfulQueryPlugin(spaceId, environment, accessToken);   
  on('task', qp);
}

module.exports = (on, config) => {
  addContentfulTasks(on, config);
  return config;
};

This adds the following cy.task commands:

cy.task('getCNFLResource', options);
cy.task('getCNFLResourceById', options);

While using this tool as a plugin is preferred, the class can also be called directly in the test files. Function calls must be wrapped with cy.wrap() for them to included correctly with Cypress's internal stack.

const contentfulPlugin = require(`crds-cypress-contentful`);

describe('Query Contentful as non-plugin', () => {
  let contentfulQuery;
  before(() => {
    const spaceId = Cypress.env('SPACE_ID');
    const environment = Cypress.env('ENVIRONMENT');
    const accessToken = Cypress.env('ACCESS_TOKEN');
    
    contentfulQuery = contentfulPlugin.ContentfulQueryPlugin(spaceId, environment, accessToken);
  });
  
  it('Makes the query', () => {
    cy.wrap(contentfulQuery.getCNFLResourceById('123'))
    .then((resource) => {
      ...
    });
  });
});

Usage

In a test, call the cy.task() command and retrieve the results from a .then() clause.

it('Fetches entries', () => {
  const seriesQuery = "content_type=series&select=fields.title,fields.slug&limit=2"
  cy.task('getCNFLResource', seriesQuery)
  .then((seriesList) => {
    expect(seriesList).to.be.an('array');
    expect(seriesList[0].title.text).to.eq('some title');
    ...
  });
});

it('Fetches entry by id', () => {
  const seriesId = "123"
  cy.task('getCNFLResourceById', seriesId)
  .then((series) => {
    expect(series).to.have.property('title');
    expect(series.title.text).to.eq('some title');
    ...
  });
});

To retrieve an asset, an options object must be used instead of a string since the cy.task() command only accepts one argument.

it('Fetches assets', () => {
  const queryParams = 'fields.contentType[contains]=image&limit=2';
  const options = { queryParams, type: 'assets'};
  cy.task('getCNFLResource', options)
  .then((imageList) => {
    expect(imageList).to.be.an('array');
    expect(imageList[0].title.text).to.eq('some title');
    ...
  });
});

it('Fetches asset by id', () => {
  const id = '123';
  const options = { id, type: 'assets'};
  cy.task('getCNFLResourceById', options)
  .then((image) => {
    expect(image).to.have.property('title');
    expect(image.title.text).to.eq('some title');
    ...
  });
});

Query Builders

These helper classes lets a user "configure" the parameters of a query to generate the query parameters needed by the getCNFLResource function. The ContentfulQueryBuilder class automatically adds a request for sys.id and sys.type if the 'select' parameter is set, and sub-classes SeriesQueryBuilder, MessageQueryBuilder and PromoQueryBuilder also add 'searchFor' parameters to ensure the results are also 'published' on crossroads.net. All these classes can be imported an used directly in the test file.

import { ContentfulQueryBuilder, SeriesQueryBuilder } from 'crds-cypress-contentful';

it('Builds a query for content blocks', () => {
  const qb = new ContentfulQueryBuilder('content_block');
  qb.searchFor = 'fields.category[exists]=true';
  qb.orderBy = '-fields.title';
  qb.select = 'fields.title,fields.slug';
  qb.limit = 2;

  const queryParams = qb.queryParams;
  cy.task('getCNFLResource', queryParams)
  .then((contentBlockList) => {
    expect(contentBlockList).to.be.an('array');
    ...
  });
});

it('Builds a query for Series', () => {
  const qb = new SeriesQueryBuilder();
  qb.orderBy = '-fields.published_at';
  qb.limit = 1;

  const queryParams = qb.queryParams;
  cy.task('getCNFLResource', queryParams)
  .then((series) => {
    expect(series).to.have.property('title');
    ...
  });
});

Text Normalizer

This helper function removes markdown, special characters, newlines and extra whitespace to make it easier to compare text retrieved from Contentful with text retrieved from a page's element. This function uses document.createElement() to interpret markdown as a browser would, so it must be run in a context where this function is available.

import { normalizeText } from 'crds-cypress-contentful';
let currentSeries;
before(() => {
  cy.task('getCNFLResourceById', '123seriesId')
  .then((series) => {
    currentSeries = series;
  });
});

it('Normalizes Contentful and element text', () => {
  cy.visit('/some-page');
  cy.get('#text-field')
  .should('have.prop', 'textContent')
  .then(normalizeText)
  .then((normalElementText))
      const normalContentfulText = normalizeText(currentSeries.description.text);
      expect(normalContentfulText).to.contain(normalElementText);
    });
});