From d3925c919a06e9fee4f971b1e02386fe73214c88 Mon Sep 17 00:00:00 2001 From: Peter Kamps Date: Sat, 26 May 2018 17:01:21 +0200 Subject: [PATCH 1/2] feat(timeout): allow to configure the timeout for queries - make sure that Cypress' timeout is larger, so our error message is used - make sure Cypress' Command Log doesn't get cluttered when options isn't specified - show example in the docs - updated outdated snapshot - ignore .idea folder (JetBrains IDE's) - added myself to contributers at the request of the contribution guide --- .all-contributorsrc | 11 +++++++++++ .gitignore | 1 + README.md | 9 ++++----- src/__tests__/__snapshots__/commands.js.snap | 4 ++++ src/index.js | 19 ++++++++++++++----- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 55d1b05..2ff0486 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -38,6 +38,17 @@ "code", "test" ] + }, + { + "login": "npeterkamps", + "name": "Peter Kamps", + "avatar_url": "https://avatars1.githubusercontent.com/u/25429764?v=4", + "profile": "https://github.com/npeterkamps", + "contributions": [ + "code", + "doc", + "ideas" + ] } ], "repoType": "github" diff --git a/.gitignore b/.gitignore index 5713cd4..11f40c6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ dist .opt-out .DS_Store .eslintcache +.idea/ # these cause more harm than good # when working with contributors diff --git a/README.md b/README.md index 21b193f..bbb59b0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] -[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] @@ -73,6 +73,7 @@ To show some simple examples (from [cypress/integration/commands.spec.js](cypres cy.getAllByText('Jackie Chan').click() cy.queryByText('Button Text').should('exist') cy.queryByText('Non-existing Button Text').should('not.exist') +cy.queryByLabelText('Label text', { timeout: 7000 }).should('exist') ``` ## Other Solutions @@ -85,11 +86,9 @@ here! Thanks goes to these people ([emoji key][emojis]): - -| [
Kent C. Dodds](https://kentcdodds.com)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Tests") | [
Ivan Babak](https://sompylasar.github.io)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=sompylasar "Code") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [
Łukasz Gandecki](http://team.thebrain.pro)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Tests") | -| :---: | :---: | :---: | - +| [
Kent C. Dodds](https://kentcdodds.com)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Tests") | [
Ivan Babak](https://sompylasar.github.io)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=sompylasar "Code") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [
Łukasz Gandecki](http://team.thebrain.pro)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Tests") | [
Peter Kamps](https://github.com/npeterkamps)
[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Documentation") [🤔](#ideas-npeterkamps "Ideas, Planning, & Feedback") | +| :---: | :---: | :---: | :---: | This project follows the [all-contributors][all-contributors] specification. diff --git a/src/__tests__/__snapshots__/commands.js.snap b/src/__tests__/__snapshots__/commands.js.snap index 3b142b5..dbebdc9 100644 --- a/src/__tests__/__snapshots__/commands.js.snap +++ b/src/__tests__/__snapshots__/commands.js.snap @@ -26,5 +26,9 @@ Array [ "queryAllByTitle", "getByTitle", "getAllByTitle", + "queryByValue", + "queryAllByValue", + "getByValue", + "getAllByValue", ] `; diff --git a/src/index.js b/src/index.js index fa4da0d..4095095 100644 --- a/src/index.js +++ b/src/index.js @@ -3,12 +3,13 @@ import {queries, waitForElement} from 'dom-testing-library' const commands = Object.keys(queries).map(queryName => { return { name: queryName, - command: (cy, ...args) => { + command: (cy, text, options = {}) => { + const { timeout = 3000 } = options; const queryImpl = queries[queryName] const baseCommandImpl = doc => - waitForElement(() => queryImpl(doc, ...args), { + waitForElement(() => queryImpl(doc, text, options), { container: doc, - timeout: 3000, + timeout, }) let commandImpl if ( @@ -32,12 +33,20 @@ const commands = Object.keys(queries).map(queryName => { )(commandImpl) return cy .window({log: false}) - .then(thenHandler) + .then({ timeout: timeout + 100 }, thenHandler) .then(subject => { Cypress.log({ $el: subject, name: queryName, - message: args, + message: [text, options].filter((value) => { + if (Array.isArray(value) && value.length === 0) { + return false; + } + if (typeof value === 'object' && Object.keys(value).length === 0) { + return false; + } + return Boolean(value); + }), }) return subject }) From 8c9c766b48b6759ff77decd28b85078dc1383beb Mon Sep 17 00:00:00 2001 From: Peter Kamps Date: Sat, 2 Jun 2018 12:45:15 +0200 Subject: [PATCH 2/2] Pass all args to `queryImpl`, pass all args with some defaults to `waitForElement`. --- src/index.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 4095095..b9fa208 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,23 @@ import {queries, waitForElement} from 'dom-testing-library' +const defaults = { + timeout: 3000, +} + const commands = Object.keys(queries).map(queryName => { return { name: queryName, - command: (cy, text, options = {}) => { - const { timeout = 3000 } = options; + command: (cy, ...args) => { + const lastArg = args[args.length - 1] + const waitOptions = (typeof lastArg === 'object') + ? Object.assign({}, defaults, lastArg) + : defaults + const queryImpl = queries[queryName] const baseCommandImpl = doc => - waitForElement(() => queryImpl(doc, text, options), { + waitForElement(() => queryImpl(doc, ...args), Object.assign({}, waitOptions, { container: doc, - timeout, - }) + })) let commandImpl if ( queryName.startsWith('queryBy') || @@ -33,12 +40,12 @@ const commands = Object.keys(queries).map(queryName => { )(commandImpl) return cy .window({log: false}) - .then({ timeout: timeout + 100 }, thenHandler) + .then({ timeout: waitOptions.timeout + 100 }, thenHandler) .then(subject => { Cypress.log({ $el: subject, name: queryName, - message: [text, options].filter((value) => { + message: args.filter((value) => { if (Array.isArray(value) && value.length === 0) { return false; }