Code Monkey home page Code Monkey logo

todo-graphql-example's Introduction

todo-graphql-example cypress version

ci status badges status tags renovate-app badge todo-graphql-example CircleCI

Blog posts

Read Smart GraphQL Stubbing in Cypress. Note that with the addition of cy.intercept all extra hacks became unnecessary.

Videos

Cypress tests

All tests are in the cypress/integration folder.

By mocking network calls using cy.intercept see the intercept-spec.js file.

Spec client-spec.js is testing making individual GraphQL calls using app's own client.

Spec ui-spec.js has simple tests that do not depend on the network, and thus are hard to write.

We can use cy.request command to make GraphQL requests ourselves, see the request-spec.js file.

We can stub the initial items load using a fixture file. See the spec file fixture-spec.js.

We delete all items in the delete-spec.js test. First we query all todo items, then delete them one by one.

We can import the list of items from a fixture file cypress/fixtures/three.json and create a dynamic test for each item, see the spec file dynamic-spec.js.

App

Start server with npm start. You can find GraphQL playground at http://localhost:3000

App in action

Example asking for all todos

query {
  allTodos {
    id,
    title,
    completed
  }
}

Response

{
  "data": {
    "allTodos": [
      {
        "id": "1",
        "title": "do something",
        "completed": false
      },
      {
        "id": "2",
        "title": "another",
        "completed": false
      }
    ]
  }
}

Example creating new todo object

mutation {
  createTodo(id: 2, title: "another", completed: false) {
    id
  }
}

Response

{
  "data": {
    "createTodo": {
      "id": "2"
    }
  }
}

Example asking for a single todo (notice id argument)

query {
  Todo(id: 2) {
    id,
    title,
    completed
  }
}

Response

{
  "data": {
    "Todo": {
      "id": "2",
      "title": "another",
      "completed": false
    }
  }
}

Development

Backend is json-graphql-server. Front-end React code is in src folder, modeled after Getting Started With React And GraphQL post.

To start the applications and open Cypress

$ npm run dev
# starts the API, starts the web application
# when the application responds
# opens Cypress test runner

To start the application and run headless Cypress tests

$ npm run local

Types

Look at cypress/jsconfig.json that loads all 3rd party types, and includes the link to cypress/support/index.d.ts where I describe the type for custom command cy.createTodos defined in cypress/support/index.js.

About me

todo-graphql-example's People

Contributors

bahmutov avatar dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

todo-graphql-example's Issues

test mocked graphql using cypress-graphql-mock

  • load 2 todo items from fixture using graphql mock
  • mock addTodo mutation (can we spy on it?)
  • mock 3 todo items load that the web app is expected to do after adding new item
  • check DOM is showing 3 items

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm cypress-grep Unavailable

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
  • cypress/base 16.14.0
github-actions
.github/workflows/badges.yml
.github/workflows/ci.yml
  • cypress-io/github-action v2
.github/workflows/grep.yml
  • cypress-io/github-action v2
.github/workflows/tags.yml
  • cypress-io/github-action v2
  • cypress-io/github-action v2
  • cypress-io/github-action v2
  • cypress-io/github-action v2
  • cypress-io/github-action v2
  • cypress-io/github-action v2
npm
package.json
  • @bahmutov/cy-api 1.6.2
  • cypress 9.7.0
  • cypress-data-session 1.15.1
  • cypress-each 1.10.0
  • cypress-grep 2.14.0
  • cypress-highlight 1.2.0
  • cypress-timestamps 1.0.2

  • Check this box to trigger a request for Renovate to run again on this repository

Idea: Add Operation Name in Headers

Hi @bahmutov - thanks for this, I just found it after reading your article here: https://glebbahmutov.com/blog/smart-graphql-stubbing/.

I am also working on doing some Cypress testing on a Graphql so would be happy to offer some input here! About 6 - 8 weeks ago I was tearing my hair out with trying to test this and felt sure there had to be an easier way than what I found at the time.

I was thinking though, since cy.route2 allows us to provide a route matcher (https://docs.cypress.io/api/commands/route2.html#routeMatcher-RouteMatcher), why not just put a x-gql-operation-name header in to our graphql requests that we can use to identity them. It's relatively easy to add this to the headers using the React Apollo client for example.

As I understand it, we'd then be able to alias the calls and use them in a relatively straight forward Cypress-y way.

What do you think, is it something you've considered before?

How to preserve mocked fetch during window reload?

it.skip('shows new item after reload', () => {
  cy.get('.todo-list li').should('have.length', 2)
  cy.get('.new-todo').type('new todo{enter}')

  cy.get('.todo-list li')
    .should('have.length', 3)
    .contains('new todo')

  // currently deletes the window.fetch mock
  cy.reload()
})

this "loses" the stubbed fetch, and breaks in-memory json-server load

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.