Merge pull request #134 from acelaya/feature/jest-mocks

Feature/jest mocks
This commit is contained in:
Alejandro Celaya 2019-04-19 13:02:28 +02:00 committed by GitHub
commit 48f7103205
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 887 additions and 558 deletions

View file

@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
#### Changed #### Changed
* [#125](https://github.com/shlinkio/shlink-web-client/issues/125) Refactored reducers to replace `switch` statements by `handleActions` from [redux-actions](https://github.com/redux-utilities/redux-actions). * [#125](https://github.com/shlinkio/shlink-web-client/issues/125) Refactored reducers to replace `switch` statements by `handleActions` from [redux-actions](https://github.com/redux-utilities/redux-actions).
* [#116](https://github.com/shlinkio/shlink-web-client/issues/116) Removed sinon in favor of jest mocks.
#### Deprecated #### Deprecated

634
package-lock.json generated
View file

@ -921,43 +921,6 @@
"integrity": "sha1-K1o6s/kYzKSKjHVMCBaOPwPrphs=", "integrity": "sha1-K1o6s/kYzKSKjHVMCBaOPwPrphs=",
"dev": true "dev": true
}, },
"@sinonjs/commons": {
"version": "1.3.0",
"resolved": "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.3.0.tgz",
"integrity": "sha1-UKJ1QBa28wqZTO2m2aCow2rdqEk=",
"dev": true,
"requires": {
"type-detect": "4.0.8"
}
},
"@sinonjs/formatio": {
"version": "3.1.0",
"resolved": "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-3.1.0.tgz",
"integrity": "sha1-asnR6xghmE2ExJlnJuRdFkbYzOU=",
"dev": true,
"requires": {
"@sinonjs/samsam": "^2 || ^3"
},
"dependencies": {
"@sinonjs/samsam": {
"version": "3.0.2",
"resolved": "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-3.0.2.tgz",
"integrity": "sha1-ME+zO9VYWgst+KTIAfy0f6hNjkM=",
"dev": true,
"requires": {
"@sinonjs/commons": "^1.0.2",
"array-from": "^2.1.1",
"lodash.get": "^4.4.2"
}
}
}
},
"@sinonjs/samsam": {
"version": "2.1.3",
"resolved": "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-2.1.3.tgz",
"integrity": "sha1-Ys8qm2JO3HlRNBNf43/Cro6ja+M=",
"dev": true
},
"@svgr/core": { "@svgr/core": {
"version": "2.4.1", "version": "2.4.1",
"resolved": "https://registry.yarnpkg.com/@svgr/core/-/core-2.4.1.tgz", "resolved": "https://registry.yarnpkg.com/@svgr/core/-/core-2.4.1.tgz",
@ -1468,12 +1431,6 @@
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=",
"dev": true "dev": true
}, },
"array-from": {
"version": "2.1.1",
"resolved": "https://registry.yarnpkg.com/array-from/-/array-from-2.1.1.tgz",
"integrity": "sha1-z+nYwmYoudxa7MYqn12PHzUsEZU=",
"dev": true
},
"array-includes": { "array-includes": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.yarnpkg.com/array-includes/-/array-includes-3.0.3.tgz", "resolved": "https://registry.yarnpkg.com/array-includes/-/array-includes-3.0.3.tgz",
@ -4296,6 +4253,7 @@
"anymatch": "^2.0.0", "anymatch": "^2.0.0",
"async-each": "^1.0.0", "async-each": "^1.0.0",
"braces": "^2.3.0", "braces": "^2.3.0",
"fsevents": "^1.2.2",
"glob-parent": "^3.1.0", "glob-parent": "^3.1.0",
"inherits": "^2.0.1", "inherits": "^2.0.1",
"is-binary-path": "^1.0.0", "is-binary-path": "^1.0.0",
@ -7042,6 +7000,535 @@
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
"dev": true "dev": true
}, },
"fsevents": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.8.tgz",
"integrity": "sha512-tPvHgPGB7m40CZ68xqFGkKuzN+RnpGmSV+hgeKxhRpbxdqKXUFJGC3yonBOLzQBcJyGpdZFDfCsdOC2KFsXzeA==",
"dev": true,
"optional": true,
"requires": {
"nan": "^2.12.1",
"node-pre-gyp": "^0.12.0"
},
"dependencies": {
"abbrev": {
"version": "1.1.1",
"bundled": true,
"dev": true,
"optional": true
},
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true
},
"aproba": {
"version": "1.2.0",
"bundled": true,
"dev": true,
"optional": true
},
"are-we-there-yet": {
"version": "1.1.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"delegates": "^1.0.0",
"readable-stream": "^2.0.6"
}
},
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
}
},
"chownr": {
"version": "1.1.1",
"bundled": true,
"dev": true,
"optional": true
},
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true
},
"core-util-is": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
},
"debug": {
"version": "4.1.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ms": "^2.1.1"
}
},
"deep-extend": {
"version": "0.6.0",
"bundled": true,
"dev": true,
"optional": true
},
"delegates": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
},
"detect-libc": {
"version": "1.0.3",
"bundled": true,
"dev": true,
"optional": true
},
"fs-minipass": {
"version": "1.2.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minipass": "^2.2.1"
}
},
"fs.realpath": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
},
"gauge": {
"version": "2.7.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"aproba": "^1.0.3",
"console-control-strings": "^1.0.0",
"has-unicode": "^2.0.0",
"object-assign": "^4.1.0",
"signal-exit": "^3.0.0",
"string-width": "^1.0.1",
"strip-ansi": "^3.0.1",
"wide-align": "^1.1.0"
}
},
"glob": {
"version": "7.1.3",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.0.4",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
}
},
"has-unicode": {
"version": "2.0.1",
"bundled": true,
"dev": true,
"optional": true
},
"iconv-lite": {
"version": "0.4.24",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
},
"ignore-walk": {
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimatch": "^3.0.4"
}
},
"inflight": {
"version": "1.0.6",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"once": "^1.3.0",
"wrappy": "1"
}
},
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true
},
"ini": {
"version": "1.3.5",
"bundled": true,
"dev": true,
"optional": true
},
"is-fullwidth-code-point": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"requires": {
"number-is-nan": "^1.0.0"
}
},
"isarray": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
},
"minimatch": {
"version": "3.0.4",
"bundled": true,
"dev": true,
"requires": {
"brace-expansion": "^1.1.7"
}
},
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
}
},
"minizlib": {
"version": "1.2.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minipass": "^2.2.1"
}
},
"mkdirp": {
"version": "0.5.1",
"bundled": true,
"dev": true,
"requires": {
"minimist": "0.0.8"
}
},
"ms": {
"version": "2.1.1",
"bundled": true,
"dev": true,
"optional": true
},
"needle": {
"version": "2.3.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"debug": "^4.1.0",
"iconv-lite": "^0.4.4",
"sax": "^1.2.4"
}
},
"node-pre-gyp": {
"version": "0.12.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"detect-libc": "^1.0.2",
"mkdirp": "^0.5.1",
"needle": "^2.2.1",
"nopt": "^4.0.1",
"npm-packlist": "^1.1.6",
"npmlog": "^4.0.2",
"rc": "^1.2.7",
"rimraf": "^2.6.1",
"semver": "^5.3.0",
"tar": "^4"
}
},
"nopt": {
"version": "4.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"abbrev": "1",
"osenv": "^0.1.4"
}
},
"npm-bundled": {
"version": "1.0.6",
"bundled": true,
"dev": true,
"optional": true
},
"npm-packlist": {
"version": "1.4.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ignore-walk": "^3.0.1",
"npm-bundled": "^1.0.1"
}
},
"npmlog": {
"version": "4.1.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"are-we-there-yet": "~1.1.2",
"console-control-strings": "~1.1.0",
"gauge": "~2.7.3",
"set-blocking": "~2.0.0"
}
},
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true
},
"object-assign": {
"version": "4.1.1",
"bundled": true,
"dev": true,
"optional": true
},
"once": {
"version": "1.4.0",
"bundled": true,
"dev": true,
"requires": {
"wrappy": "1"
}
},
"os-homedir": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
},
"os-tmpdir": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
},
"osenv": {
"version": "0.1.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"os-homedir": "^1.0.0",
"os-tmpdir": "^1.0.0"
}
},
"path-is-absolute": {
"version": "1.0.1",
"bundled": true,
"dev": true,
"optional": true
},
"process-nextick-args": {
"version": "2.0.0",
"bundled": true,
"dev": true,
"optional": true
},
"rc": {
"version": "1.2.8",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"deep-extend": "^0.6.0",
"ini": "~1.3.0",
"minimist": "^1.2.0",
"strip-json-comments": "~2.0.1"
},
"dependencies": {
"minimist": {
"version": "1.2.0",
"bundled": true,
"dev": true,
"optional": true
}
}
},
"readable-stream": {
"version": "2.3.6",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"rimraf": {
"version": "2.6.3",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"glob": "^7.1.3"
}
},
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
"bundled": true,
"dev": true,
"optional": true
},
"sax": {
"version": "1.2.4",
"bundled": true,
"dev": true,
"optional": true
},
"semver": {
"version": "5.7.0",
"bundled": true,
"dev": true,
"optional": true
},
"set-blocking": {
"version": "2.0.0",
"bundled": true,
"dev": true,
"optional": true
},
"signal-exit": {
"version": "3.0.2",
"bundled": true,
"dev": true,
"optional": true
},
"string-width": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
"strip-ansi": "^3.0.0"
}
},
"string_decoder": {
"version": "1.1.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "~5.1.0"
}
},
"strip-ansi": {
"version": "3.0.1",
"bundled": true,
"dev": true,
"requires": {
"ansi-regex": "^2.0.0"
}
},
"strip-json-comments": {
"version": "2.0.1",
"bundled": true,
"dev": true,
"optional": true
},
"tar": {
"version": "4.4.8",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"chownr": "^1.1.1",
"fs-minipass": "^1.2.5",
"minipass": "^2.3.4",
"minizlib": "^1.1.1",
"mkdirp": "^0.5.0",
"safe-buffer": "^5.1.2",
"yallist": "^3.0.2"
}
},
"util-deprecate": {
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true
},
"wide-align": {
"version": "1.1.3",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"string-width": "^1.0.2 || 2"
}
},
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
"dev": true
}
}
},
"fstream": { "fstream": {
"version": "1.0.11", "version": "1.0.11",
"resolved": "https://registry.yarnpkg.com/fstream/-/fstream-1.0.11.tgz", "resolved": "https://registry.yarnpkg.com/fstream/-/fstream-1.0.11.tgz",
@ -9725,12 +10212,6 @@
"resolved": "https://registry.yarnpkg.com/just-curry-it/-/just-curry-it-3.1.0.tgz", "resolved": "https://registry.yarnpkg.com/just-curry-it/-/just-curry-it-3.1.0.tgz",
"integrity": "sha1-q1na7TCKWLhHraFm7dCi1Adm+8U=" "integrity": "sha1-q1na7TCKWLhHraFm7dCi1Adm+8U="
}, },
"just-extend": {
"version": "4.0.2",
"resolved": "https://registry.yarnpkg.com/just-extend/-/just-extend-4.0.2.tgz",
"integrity": "sha1-8/R/ffyg+YnFVBCn68iFSwcQivw=",
"dev": true
},
"killable": { "killable": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.yarnpkg.com/killable/-/killable-1.0.1.tgz", "resolved": "https://registry.yarnpkg.com/killable/-/killable-1.0.1.tgz",
@ -9966,12 +10447,6 @@
"integrity": "sha1-+wMJF/hqMTTlvJvsDWngAT3f7bI=", "integrity": "sha1-+wMJF/hqMTTlvJvsDWngAT3f7bI=",
"dev": true "dev": true
}, },
"lodash.get": {
"version": "4.4.2",
"resolved": "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=",
"dev": true
},
"lodash.isequal": { "lodash.isequal": {
"version": "4.5.0", "version": "4.5.0",
"resolved": "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz", "resolved": "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz",
@ -10057,12 +10532,6 @@
"integrity": "sha1-4PyVEztu8nbNyIh82vJKpvFW+Po=", "integrity": "sha1-4PyVEztu8nbNyIh82vJKpvFW+Po=",
"dev": true "dev": true
}, },
"lolex": {
"version": "2.7.5",
"resolved": "https://registry.yarnpkg.com/lolex/-/lolex-2.7.5.tgz",
"integrity": "sha1-ETAB1Wv8fgLVbjYpHMXEE9GqBzM=",
"dev": true
},
"longest-streak": { "longest-streak": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.yarnpkg.com/longest-streak/-/longest-streak-2.0.2.tgz", "resolved": "https://registry.yarnpkg.com/longest-streak/-/longest-streak-2.0.2.tgz",
@ -10726,19 +11195,6 @@
"integrity": "sha1-ozeKdpbOfSI+iPybdkvX7xCJ42Y=", "integrity": "sha1-ozeKdpbOfSI+iPybdkvX7xCJ42Y=",
"dev": true "dev": true
}, },
"nise": {
"version": "1.4.8",
"resolved": "https://registry.yarnpkg.com/nise/-/nise-1.4.8.tgz",
"integrity": "sha1-zpHDHobPmyxMrEnX/Nf1Z3m/1rA=",
"dev": true,
"requires": {
"@sinonjs/formatio": "^3.1.0",
"just-extend": "^4.0.2",
"lolex": "^2.3.2",
"path-to-regexp": "^1.7.0",
"text-encoding": "^0.6.4"
}
},
"no-case": { "no-case": {
"version": "2.3.2", "version": "2.3.2",
"resolved": "https://registry.yarnpkg.com/no-case/-/no-case-2.3.2.tgz", "resolved": "https://registry.yarnpkg.com/no-case/-/no-case-2.3.2.tgz",
@ -14185,6 +14641,7 @@
"capture-exit": "^1.2.0", "capture-exit": "^1.2.0",
"exec-sh": "^0.2.0", "exec-sh": "^0.2.0",
"fb-watchman": "^2.0.0", "fb-watchman": "^2.0.0",
"fsevents": "^1.2.3",
"micromatch": "^3.1.4", "micromatch": "^3.1.4",
"minimist": "^1.1.1", "minimist": "^1.1.1",
"walker": "~1.0.5", "walker": "~1.0.5",
@ -14668,23 +15125,6 @@
} }
} }
}, },
"sinon": {
"version": "6.3.5",
"resolved": "https://registry.yarnpkg.com/sinon/-/sinon-6.3.5.tgz",
"integrity": "sha1-D21qW066rR9ujgGTlVQtHQLBRKA=",
"dev": true,
"requires": {
"@sinonjs/commons": "^1.0.2",
"@sinonjs/formatio": "^3.0.0",
"@sinonjs/samsam": "^2.1.2",
"diff": "^3.5.0",
"lodash.get": "^4.4.2",
"lolex": "^2.7.5",
"nise": "^1.4.5",
"supports-color": "^5.5.0",
"type-detect": "^4.0.8"
}
},
"sisteransi": { "sisteransi": {
"version": "0.1.1", "version": "0.1.1",
"resolved": "https://registry.yarnpkg.com/sisteransi/-/sisteransi-0.1.1.tgz", "resolved": "https://registry.yarnpkg.com/sisteransi/-/sisteransi-0.1.1.tgz",
@ -15843,12 +16283,6 @@
} }
} }
}, },
"text-encoding": {
"version": "0.6.4",
"resolved": "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz",
"integrity": "sha1-45mpgiV6J22uQou5KEXLcb3CbRk=",
"dev": true
},
"text-table": { "text-table": {
"version": "0.2.0", "version": "0.2.0",
"resolved": "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz", "resolved": "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz",
@ -16137,12 +16571,6 @@
"prelude-ls": "~1.1.2" "prelude-ls": "~1.1.2"
} }
}, },
"type-detect": {
"version": "4.0.8",
"resolved": "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz",
"integrity": "sha1-dkb7XxiHHPu3dJ5pvTmmOI63RQw=",
"dev": true
},
"type-is": { "type-is": {
"version": "1.6.16", "version": "1.6.16",
"resolved": "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz", "resolved": "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz",

View file

@ -113,7 +113,6 @@
"resolve": "^1.8.1", "resolve": "^1.8.1",
"sass-loader": "^7.1.0", "sass-loader": "^7.1.0",
"serve": "^10.0.0", "serve": "^10.0.0",
"sinon": "^6.1.5",
"style-loader": "^0.23.0", "style-loader": "^0.23.0",
"stylelint": "^9.9.0", "stylelint": "^9.9.0",
"stylelint-config-adidas": "^1.2.1", "stylelint-config-adidas": "^1.2.1",

View file

@ -19,7 +19,6 @@ describe('<App />', () => {
it('renders app main routes', () => { it('renders app main routes', () => {
const routes = wrapper.find(Route); const routes = wrapper.find(Route);
const expectedRoutesCount = 4;
const expectedPaths = [ const expectedPaths = [
'/server/create', '/server/create',
'/', '/',
@ -27,7 +26,7 @@ describe('<App />', () => {
]; ];
expect.assertions(expectedPaths.length + 1); expect.assertions(expectedPaths.length + 1);
expect(routes).toHaveLength(expectedRoutesCount); expect(routes).toHaveLength(4);
expectedPaths.forEach((path, index) => { expectedPaths.forEach((path, index) => {
expect(routes.at(index).prop('path')).toEqual(path); expect(routes.at(index).prop('path')).toEqual(path);
}); });

View file

@ -16,9 +16,8 @@ describe('<AsideMenu />', () => {
it('contains links to different sections', () => { it('contains links to different sections', () => {
const links = wrapped.find(NavLink); const links = wrapped.find(NavLink);
const expectedLength = 3;
expect(links).toHaveLength(expectedLength); expect(links).toHaveLength(3);
links.forEach((link) => expect(link.prop('to')).toContain('abc123')); links.forEach((link) => expect(link.prop('to')).toContain('abc123'));
}); });

View file

@ -1,7 +1,6 @@
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { values } from 'ramda'; import { values } from 'ramda';
import React from 'react'; import React from 'react';
import * as sinon from 'sinon';
import Home from '../../src/common/Home'; import Home from '../../src/common/Home';
describe('<Home />', () => { describe('<Home />', () => {
@ -28,11 +27,11 @@ describe('<Home />', () => {
}); });
it('resets selected server when mounted', () => { it('resets selected server when mounted', () => {
const resetSelectedServer = sinon.spy(); const resetSelectedServer = jest.fn();
expect(resetSelectedServer.called).toEqual(false); expect(resetSelectedServer).not.toHaveBeenCalled();
createComponent({ resetSelectedServer }); createComponent({ resetSelectedServer });
expect(resetSelectedServer.called).toEqual(true); expect(resetSelectedServer).toHaveBeenCalled();
}); });
it('shows link to create server when no servers exist', () => { it('shows link to create server when no servers exist', () => {

View file

@ -1,12 +1,11 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import createScrollToTop from '../../src/common/ScrollToTop'; import createScrollToTop from '../../src/common/ScrollToTop';
describe('<ScrollToTop />', () => { describe('<ScrollToTop />', () => {
let wrapper; let wrapper;
const window = { const window = {
scrollTo: sinon.spy(), scrollTo: jest.fn(),
}; };
beforeEach(() => { beforeEach(() => {
@ -17,13 +16,13 @@ describe('<ScrollToTop />', () => {
afterEach(() => { afterEach(() => {
wrapper.unmount(); wrapper.unmount();
window.scrollTo.resetHistory(); window.scrollTo.mockReset();
}); });
it('just renders children', () => expect(wrapper.text()).toEqual('Foobar')); it('just renders children', () => expect(wrapper.text()).toEqual('Foobar'));
it('scrolls to top when location changes', () => { it('scrolls to top when location changes', () => {
wrapper.instance().componentDidUpdate({ location: { href: 'bar' } }); wrapper.instance().componentDidUpdate({ location: { href: 'bar' } });
expect(window.scrollTo.calledOnce).toEqual(true); expect(window.scrollTo).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -1,20 +1,18 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import sinon from 'sinon';
import createServerConstruct from '../../src/servers/CreateServer'; import createServerConstruct from '../../src/servers/CreateServer';
describe('<CreateServer />', () => { describe('<CreateServer />', () => {
let wrapper; let wrapper;
const ImportServersBtn = () => ''; const ImportServersBtn = () => '';
const createServerMock = sinon.fake(); const createServerMock = jest.fn();
const historyMock = { const historyMock = {
push: sinon.fake(), push: jest.fn(),
}; };
beforeEach(() => { beforeEach(() => {
createServerMock.resetHistory(); createServerMock.mockReset();
historyMock.push.resetHistory();
const CreateServer = createServerConstruct(ImportServersBtn); const CreateServer = createServerConstruct(ImportServersBtn);
@ -44,8 +42,8 @@ describe('<CreateServer />', () => {
return ''; return '';
} }); } });
expect(createServerMock.callCount).toEqual(1); expect(createServerMock).toHaveBeenCalledTimes(1);
expect(historyMock.push.callCount).toEqual(1); expect(historyMock.push).toHaveBeenCalledTimes(1);
}); });
it('updates state when inputs are changed', () => { it('updates state when inputs are changed', () => {

View file

@ -1,20 +1,19 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import sinon from 'sinon';
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap'; import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
import DeleteServerModal from '../../src/servers/DeleteServerModal'; import DeleteServerModal from '../../src/servers/DeleteServerModal';
describe('<DeleteServerModal />', () => { describe('<DeleteServerModal />', () => {
let wrapper; let wrapper;
const deleteServerMock = sinon.fake(); const deleteServerMock = jest.fn();
const historyMock = { push: sinon.fake() }; const historyMock = { push: jest.fn() };
const toggleMock = sinon.fake(); const toggleMock = jest.fn();
const serverName = 'the_server_name'; const serverName = 'the_server_name';
beforeEach(() => { beforeEach(() => {
toggleMock.resetHistory(); deleteServerMock.mockReset();
deleteServerMock.resetHistory(); toggleMock.mockReset();
historyMock.push.resetHistory(); historyMock.push.mockReset();
wrapper = shallow( wrapper = shallow(
<DeleteServerModal <DeleteServerModal
@ -48,9 +47,9 @@ describe('<DeleteServerModal />', () => {
cancelBtn.simulate('click'); cancelBtn.simulate('click');
expect(toggleMock.callCount).toEqual(1); expect(toggleMock).toHaveBeenCalledTimes(1);
expect(deleteServerMock.callCount).toEqual(0); expect(deleteServerMock).not.toHaveBeenCalled();
expect(historyMock.push.callCount).toEqual(0); expect(historyMock.push).not.toHaveBeenCalled();
}); });
it('deletes server when clicking accept button', () => { it('deletes server when clicking accept button', () => {
@ -58,8 +57,8 @@ describe('<DeleteServerModal />', () => {
acceptBtn.simulate('click'); acceptBtn.simulate('click');
expect(toggleMock.callCount).toEqual(1); expect(toggleMock).toHaveBeenCalledTimes(1);
expect(deleteServerMock.callCount).toEqual(1); expect(deleteServerMock).toHaveBeenCalledTimes(1);
expect(historyMock.push.callCount).toEqual(1); expect(historyMock.push).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -1,25 +1,24 @@
import React from 'react'; import React from 'react';
import sinon from 'sinon';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { UncontrolledTooltip } from 'reactstrap'; import { UncontrolledTooltip } from 'reactstrap';
import importServersBtnConstruct from '../../../src/servers/helpers/ImportServersBtn'; import importServersBtnConstruct from '../../../src/servers/helpers/ImportServersBtn';
describe('<ImportServersBtn />', () => { describe('<ImportServersBtn />', () => {
let wrapper; let wrapper;
const onImportMock = sinon.fake(); const onImportMock = jest.fn();
const createServersMock = sinon.fake(); const createServersMock = jest.fn();
const serversImporterMock = { const serversImporterMock = {
importServersFromFile: sinon.fake.returns(Promise.resolve([])), importServersFromFile: jest.fn().mockResolvedValue([]),
}; };
const fileRef = { const fileRef = {
current: { click: sinon.fake() }, current: { click: jest.fn() },
}; };
beforeEach(() => { beforeEach(() => {
onImportMock.resetHistory(); onImportMock.mockReset();
createServersMock.resetHistory(); createServersMock.mockReset();
serversImporterMock.importServersFromFile.resetHistory(); serversImporterMock.importServersFromFile.mockClear();
fileRef.current.click.resetHistory(); fileRef.current.click.mockReset();
const ImportServersBtn = importServersBtnConstruct(serversImporterMock); const ImportServersBtn = importServersBtnConstruct(serversImporterMock);
@ -40,7 +39,7 @@ describe('<ImportServersBtn />', () => {
btn.simulate('click'); btn.simulate('click');
expect(fileRef.current.click.callCount).toEqual(1); expect(fileRef.current.click).toHaveBeenCalledTimes(1);
}); });
it('imports servers when file input changes', (done) => { it('imports servers when file input changes', (done) => {
@ -49,9 +48,9 @@ describe('<ImportServersBtn />', () => {
file.simulate('change', { target: { files: [ '' ] } }); file.simulate('change', { target: { files: [ '' ] } });
setImmediate(() => { setImmediate(() => {
expect(serversImporterMock.importServersFromFile.callCount).toEqual(1); expect(serversImporterMock.importServersFromFile).toHaveBeenCalledTimes(1);
expect(createServersMock.callCount).toEqual(1); expect(createServersMock).toHaveBeenCalledTimes(1);
expect(onImportMock.callCount).toEqual(1); expect(onImportMock).toHaveBeenCalledTimes(1);
done(); done();
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
selectServer, selectServer,
resetSelectedServer, resetSelectedServer,
@ -31,31 +30,27 @@ describe('selectedServerReducer', () => {
id: serverId, id: serverId,
}; };
const ServersServiceMock = { const ServersServiceMock = {
findServerById: sinon.fake.returns(selectedServer), findServerById: jest.fn(() => selectedServer),
}; };
afterEach(() => { afterEach(() => {
ServersServiceMock.findServerById.resetHistory(); ServersServiceMock.findServerById.mockClear();
}); });
it('dispatches proper actions', () => { it('dispatches proper actions', () => {
const dispatch = sinon.spy(); const dispatch = jest.fn();
const expectedDispatchCalls = 2;
selectServer(ServersServiceMock)(serverId)(dispatch); selectServer(ServersServiceMock)(serverId)(dispatch);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.firstCall.calledWith({ type: RESET_SHORT_URL_PARAMS })).toEqual(true); expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SHORT_URL_PARAMS });
expect(dispatch.secondCall.calledWith({ expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer });
type: SELECT_SERVER,
selectedServer,
})).toEqual(true);
}); });
it('invokes dependencies', () => { it('invokes dependencies', () => {
selectServer(ServersServiceMock)(serverId)(() => {}); selectServer(ServersServiceMock)(serverId)(() => {});
expect(ServersServiceMock.findServerById.callCount).toEqual(1); expect(ServersServiceMock.findServerById).toHaveBeenCalledTimes(1);
}); });
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import { values } from 'ramda'; import { values } from 'ramda';
import reducer, { import reducer, {
createServer, createServer,
@ -15,10 +14,10 @@ describe('serverReducer', () => {
}; };
const expectedFetchServersResult = { type: FETCH_SERVERS, payload }; const expectedFetchServersResult = { type: FETCH_SERVERS, payload };
const ServersServiceMock = { const ServersServiceMock = {
listServers: sinon.fake.returns(payload), listServers: jest.fn(() => payload),
createServer: sinon.fake(), createServer: jest.fn(),
deleteServer: sinon.fake(), deleteServer: jest.fn(),
createServers: sinon.fake(), createServers: jest.fn(),
}; };
describe('reducer', () => { describe('reducer', () => {
@ -28,10 +27,10 @@ describe('serverReducer', () => {
describe('action creators', () => { describe('action creators', () => {
beforeEach(() => { beforeEach(() => {
ServersServiceMock.listServers.resetHistory(); ServersServiceMock.listServers.mockClear();
ServersServiceMock.createServer.resetHistory(); ServersServiceMock.createServer.mockReset();
ServersServiceMock.deleteServer.resetHistory(); ServersServiceMock.deleteServer.mockReset();
ServersServiceMock.createServers.resetHistory(); ServersServiceMock.createServers.mockReset();
}); });
describe('listServers', () => { describe('listServers', () => {
@ -39,10 +38,10 @@ describe('serverReducer', () => {
const result = listServers(ServersServiceMock)(); const result = listServers(ServersServiceMock)();
expect(result).toEqual(expectedFetchServersResult); expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.listServers.calledOnce).toEqual(true); expect(ServersServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServer.called).toEqual(false); expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer.called).toEqual(false); expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers.called).toEqual(false); expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
}); });
}); });
@ -52,11 +51,11 @@ describe('serverReducer', () => {
const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate); const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate);
expect(result).toEqual(expectedFetchServersResult); expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.createServer.calledOnce).toEqual(true); expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServer.firstCall.calledWith(serverToCreate)).toEqual(true); expect(ServersServiceMock.createServer).toHaveBeenCalledWith(serverToCreate);
expect(ServersServiceMock.listServers.called).toEqual(false); expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer.called).toEqual(false); expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers.called).toEqual(false); expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
}); });
}); });
@ -66,11 +65,11 @@ describe('serverReducer', () => {
const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete); const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete);
expect(result).toEqual(expectedFetchServersResult); expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.listServers.called).toEqual(false); expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.createServer.called).toEqual(false); expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers.called).toEqual(false); expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer.calledOnce).toEqual(true); expect(ServersServiceMock.deleteServer).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.deleteServer.firstCall.calledWith(serverToDelete)).toEqual(true); expect(ServersServiceMock.deleteServer).toHaveBeenCalledWith(serverToDelete);
}); });
}); });
@ -80,11 +79,11 @@ describe('serverReducer', () => {
const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate); const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate);
expect(result).toEqual(expectedFetchServersResult); expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.listServers.called).toEqual(false); expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.createServer.called).toEqual(false); expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers.calledOnce).toEqual(true); expect(ServersServiceMock.createServers).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServers.firstCall.calledWith(serversToCreate)).toEqual(true); expect(ServersServiceMock.createServers).toHaveBeenCalledWith(serversToCreate);
expect(ServersServiceMock.deleteServer.called).toEqual(false); expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
}); });
}); });
}); });

View file

@ -1,26 +1,25 @@
import sinon from 'sinon';
import ServersExporter from '../../../src/servers/services/ServersExporter'; import ServersExporter from '../../../src/servers/services/ServersExporter';
describe('ServersExporter', () => { describe('ServersExporter', () => {
const createLinkMock = () => ({ const createLinkMock = () => ({
setAttribute: sinon.fake(), setAttribute: jest.fn(),
click: sinon.fake(), click: jest.fn(),
style: {}, style: {},
}); });
const createWindowMock = (isIe10 = true) => ({ const createWindowMock = (isIe10 = true) => ({
navigator: { navigator: {
msSaveBlob: isIe10 ? sinon.fake() : undefined, msSaveBlob: isIe10 ? jest.fn() : undefined,
}, },
document: { document: {
createElement: sinon.fake.returns(createLinkMock()), createElement: jest.fn(() => createLinkMock()),
body: { body: {
appendChild: sinon.fake(), appendChild: jest.fn(),
removeChild: sinon.fake(), removeChild: jest.fn(),
}, },
}, },
}); });
const serversServiceMock = { const serversServiceMock = {
listServers: sinon.fake.returns({ listServers: jest.fn(() => ({
abc123: { abc123: {
id: 'abc123', id: 'abc123',
name: 'foo', name: 'foo',
@ -29,10 +28,16 @@ describe('ServersExporter', () => {
id: 'def456', id: 'def456',
name: 'bar', name: 'bar',
}, },
}), })),
}; };
const createCsvjsonMock = (throwError = false) => ({ const createCsvjsonMock = (throwError = false) => ({
toCSV: throwError ? sinon.fake.throws('') : sinon.fake.returns(''), toCSV: jest.fn(() => {
if (throwError) {
throw new Error('');
}
return '';
}),
}); });
describe('exportServers', () => { describe('exportServers', () => {
@ -40,10 +45,10 @@ describe('ServersExporter', () => {
beforeEach(() => { beforeEach(() => {
originalConsole = global.console; originalConsole = global.console;
global.console = { error: sinon.fake() }; global.console = { error: jest.fn() };
global.Blob = class Blob {}; global.Blob = class Blob {};
global.URL = { createObjectURL: () => '' }; global.URL = { createObjectURL: () => '' };
serversServiceMock.listServers.resetHistory(); serversServiceMock.listServers.mockReset();
}); });
afterEach(() => { afterEach(() => {
global.console = originalConsole; global.console = originalConsole;
@ -59,8 +64,8 @@ describe('ServersExporter', () => {
exporter.exportServers(); exporter.exportServers();
expect(global.console.error.callCount).toEqual(1); expect(global.console.error).toHaveBeenCalledTimes(1);
expect(csvjsonMock.toCSV.callCount).toEqual(1); expect(csvjsonMock.toCSV).toHaveBeenCalledTimes(1);
}); });
it('makes use of msSaveBlob API when available', () => { it('makes use of msSaveBlob API when available', () => {
@ -73,9 +78,9 @@ describe('ServersExporter', () => {
exporter.exportServers(); exporter.exportServers();
expect(serversServiceMock.listServers.callCount).toEqual(1); expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(windowMock.navigator.msSaveBlob.callCount).toEqual(1); expect(windowMock.navigator.msSaveBlob).toHaveBeenCalledTimes(1);
expect(windowMock.document.createElement.callCount).toEqual(0); expect(windowMock.document.createElement).not.toHaveBeenCalled();
}); });
it('makes use of download link API when available', () => { it('makes use of download link API when available', () => {
@ -88,10 +93,10 @@ describe('ServersExporter', () => {
exporter.exportServers(); exporter.exportServers();
expect(serversServiceMock.listServers.callCount).toEqual(1); expect(serversServiceMock.listServers).toHaveBeenCalledTimes(1);
expect(windowMock.document.createElement.callCount).toEqual(1); expect(windowMock.document.createElement).toHaveBeenCalledTimes(1);
expect(windowMock.document.body.appendChild.callCount).toEqual(1); expect(windowMock.document.body.appendChild).toHaveBeenCalledTimes(1);
expect(windowMock.document.body.removeChild.callCount).toEqual(1); expect(windowMock.document.body.removeChild).toHaveBeenCalledTimes(1);
}); });
}); });
}); });

View file

@ -1,14 +1,13 @@
import sinon from 'sinon';
import ServersImporter from '../../../src/servers/services/ServersImporter'; import ServersImporter from '../../../src/servers/services/ServersImporter';
describe('ServersImporter', () => { describe('ServersImporter', () => {
const servers = [{ name: 'foo' }, { name: 'bar' }]; const servers = [{ name: 'foo' }, { name: 'bar' }];
const csvjsonMock = { const csvjsonMock = {
toObject: sinon.fake.returns(servers), toObject: jest.fn(() => servers),
}; };
const importer = new ServersImporter(csvjsonMock); const importer = new ServersImporter(csvjsonMock);
beforeEach(() => csvjsonMock.toObject.resetHistory()); beforeEach(() => csvjsonMock.toObject.mockClear());
describe('importServersFromFile', () => { describe('importServersFromFile', () => {
it('rejects with error if no file was provided', async () => { it('rejects with error if no file was provided', async () => {
@ -28,7 +27,7 @@ describe('ServersImporter', () => {
}); });
it('reads file when a CSV is provided', async () => { it('reads file when a CSV is provided', async () => {
const readAsText = sinon.fake.returns(''); const readAsText = jest.fn(() => '');
global.FileReader = class FileReader { global.FileReader = class FileReader {
constructor() { constructor() {
@ -40,8 +39,8 @@ describe('ServersImporter', () => {
await importer.importServersFromFile({ type: 'text/csv' }); await importer.importServersFromFile({ type: 'text/csv' });
expect(readAsText.callCount).toEqual(1); expect(readAsText).toHaveBeenCalledTimes(1);
expect(csvjsonMock.toObject.callCount).toEqual(1); expect(csvjsonMock.toObject).toHaveBeenCalledTimes(1);
}); });
}); });
}); });

View file

@ -1,5 +1,3 @@
import sinon from 'sinon';
import { last } from 'ramda';
import ServersService from '../../../src/servers/services/ServersService'; import ServersService from '../../../src/servers/services/ServersService';
describe('ServersService', () => { describe('ServersService', () => {
@ -8,8 +6,8 @@ describe('ServersService', () => {
def456: { id: 'def456' }, def456: { id: 'def456' },
}; };
const createStorageMock = (returnValue) => ({ const createStorageMock = (returnValue) => ({
set: sinon.fake(), set: jest.fn(),
get: sinon.fake.returns(returnValue), get: jest.fn(() => returnValue),
}); });
describe('listServers', () => { describe('listServers', () => {
@ -20,8 +18,8 @@ describe('ServersService', () => {
const result = service.listServers(); const result = service.listServers();
expect(result).toEqual({}); expect(result).toEqual({});
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(0); expect(storageMock.set).not.toHaveBeenCalled();
}); });
it('returns value from storage when found', () => { it('returns value from storage when found', () => {
@ -31,8 +29,8 @@ describe('ServersService', () => {
const result = service.listServers(); const result = service.listServers();
expect(result).toEqual(servers); expect(result).toEqual(servers);
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(0); expect(storageMock.set).not.toHaveBeenCalled();
}); });
}); });
@ -44,8 +42,8 @@ describe('ServersService', () => {
const result = service.findServerById('ghi789'); const result = service.findServerById('ghi789');
expect(result).toBeUndefined(); expect(result).toBeUndefined();
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(0); expect(storageMock.set).not.toHaveBeenCalled();
}); });
it('returns server from list when found', () => { it('returns server from list when found', () => {
@ -55,8 +53,8 @@ describe('ServersService', () => {
const result = service.findServerById('abc123'); const result = service.findServerById('abc123');
expect(result).toEqual({ id: 'abc123' }); expect(result).toEqual({ id: 'abc123' });
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(0); expect(storageMock.set).not.toHaveBeenCalled();
}); });
}); });
@ -67,9 +65,9 @@ describe('ServersService', () => {
service.createServer({ id: 'ghi789' }); service.createServer({ id: 'ghi789' });
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(last(storageMock.set.lastCall.args)).toEqual({ expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
@ -84,9 +82,9 @@ describe('ServersService', () => {
service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]); service.createServers([{ id: 'ghi789' }, { id: 'jkl123' }]);
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(last(storageMock.set.lastCall.args)).toEqual({ expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
@ -102,9 +100,9 @@ describe('ServersService', () => {
service.deleteServer({ id: 'abc123' }); service.deleteServer({ id: 'abc123' });
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set.callCount).toEqual(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(last(storageMock.set.lastCall.args)).toEqual({ expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
def456: { id: 'def456' }, def456: { id: 'def456' },
}); });
}); });

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import moment from 'moment'; import moment from 'moment';
import * as sinon from 'sinon';
import { identity } from 'ramda'; import { identity } from 'ramda';
import createShortUrlsCreator from '../../src/short-urls/CreateShortUrl'; import createShortUrlsCreator from '../../src/short-urls/CreateShortUrl';
import DateInput from '../../src/utils/DateInput'; import DateInput from '../../src/utils/DateInput';
@ -12,7 +11,7 @@ describe('<CreateShortUrl />', () => {
const shortUrlCreationResult = { const shortUrlCreationResult = {
loading: false, loading: false,
}; };
const createShortUrl = sinon.spy(); const createShortUrl = jest.fn();
beforeEach(() => { beforeEach(() => {
const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => ''); const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => '');
@ -23,7 +22,7 @@ describe('<CreateShortUrl />', () => {
}); });
afterEach(() => { afterEach(() => {
wrapper.unmount(); wrapper.unmount();
createShortUrl.resetHistory(); createShortUrl.mockReset();
}); });
it('saves short URL with data set in form controls', (done) => { it('saves short URL with data set in form controls', (done) => {
@ -49,10 +48,8 @@ describe('<CreateShortUrl />', () => {
const form = wrapper.find('form'); const form = wrapper.find('form');
form.simulate('submit', { preventDefault: identity }); form.simulate('submit', { preventDefault: identity });
expect(createShortUrl.callCount).toEqual(1); expect(createShortUrl).toHaveBeenCalledTimes(1);
expect(createShortUrl.getCall(0).args).toEqual( expect(createShortUrl).toHaveBeenCalledWith({
[
{
longUrl: 'https://long-domain.com/foo/bar', longUrl: 'https://long-domain.com/foo/bar',
tags: [ 'tag_foo', 'tag_bar' ], tags: [ 'tag_foo', 'tag_bar' ],
customSlug: 'my-slug', customSlug: 'my-slug',
@ -60,9 +57,7 @@ describe('<CreateShortUrl />', () => {
validUntil: validUntil.format(), validUntil: validUntil.format(),
maxVisits: '20', maxVisits: '20',
findIfExists: false, findIfExists: false,
}, });
]
);
done(); done();
}); });
}); });

View file

@ -1,21 +1,17 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import sinon from 'sinon';
import searchBarCreator from '../../src/short-urls/SearchBar'; import searchBarCreator from '../../src/short-urls/SearchBar';
import SearchField from '../../src/utils/SearchField'; import SearchField from '../../src/utils/SearchField';
import Tag from '../../src/tags/helpers/Tag'; import Tag from '../../src/tags/helpers/Tag';
describe('<SearchBar />', () => { describe('<SearchBar />', () => {
let wrapper; let wrapper;
const listShortUrlsMock = sinon.spy(); const listShortUrlsMock = jest.fn();
const SearchBar = searchBarCreator({}); const SearchBar = searchBarCreator({});
afterEach(() => { afterEach(() => {
listShortUrlsMock.resetHistory(); listShortUrlsMock.mockReset();
wrapper && wrapper.unmount();
if (wrapper) {
wrapper.unmount();
}
}); });
it('renders a SearchField', () => { it('renders a SearchField', () => {
@ -42,9 +38,9 @@ describe('<SearchBar />', () => {
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />); wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
const searchField = wrapper.find(SearchField); const searchField = wrapper.find(SearchField);
expect(listShortUrlsMock.callCount).toEqual(0); expect(listShortUrlsMock).not.toHaveBeenCalled();
searchField.simulate('change'); searchField.simulate('change');
expect(listShortUrlsMock.callCount).toEqual(1); expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
}); });
it('updates short URLs list when a tag is removed', () => { it('updates short URLs list when a tag is removed', () => {
@ -53,8 +49,8 @@ describe('<SearchBar />', () => {
); );
const tag = wrapper.find(Tag).first(); const tag = wrapper.find(Tag).first();
expect(listShortUrlsMock.callCount).toEqual(0); expect(listShortUrlsMock).not.toHaveBeenCalled();
tag.simulate('close'); tag.simulate('close');
expect(listShortUrlsMock.callCount).toEqual(1); expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -3,12 +3,11 @@ import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import { CopyToClipboard } from 'react-copy-to-clipboard'; import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Tooltip } from 'reactstrap'; import { Tooltip } from 'reactstrap';
import * as sinon from 'sinon';
import createCreateShortUrlResult from '../../../src/short-urls/helpers/CreateShortUrlResult'; import createCreateShortUrlResult from '../../../src/short-urls/helpers/CreateShortUrlResult';
describe('<CreateShortUrlResult />', () => { describe('<CreateShortUrlResult />', () => {
let wrapper; let wrapper;
const stateFlagTimeout = sinon.spy(); const stateFlagTimeout = jest.fn();
const createWrapper = (result, error = false) => { const createWrapper = (result, error = false) => {
const CreateShortUrlResult = createCreateShortUrlResult(stateFlagTimeout); const CreateShortUrlResult = createCreateShortUrlResult(stateFlagTimeout);
@ -18,7 +17,7 @@ describe('<CreateShortUrlResult />', () => {
}; };
afterEach(() => { afterEach(() => {
stateFlagTimeout.resetHistory(); stateFlagTimeout.mockReset();
wrapper && wrapper.unmount(); wrapper && wrapper.unmount();
}); });
@ -48,8 +47,8 @@ describe('<CreateShortUrlResult />', () => {
const wrapper = createWrapper({ shortUrl: 'https://doma.in/abc123' }); const wrapper = createWrapper({ shortUrl: 'https://doma.in/abc123' });
const copyBtn = wrapper.find(CopyToClipboard); const copyBtn = wrapper.find(CopyToClipboard);
expect(stateFlagTimeout.callCount).toEqual(0); expect(stateFlagTimeout).not.toHaveBeenCalled();
copyBtn.simulate('copy'); copyBtn.simulate('copy');
expect(stateFlagTimeout.callCount).toEqual(1); expect(stateFlagTimeout).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import * as sinon from 'sinon';
import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal'; import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal';
describe('<DeleteShortUrlModal />', () => { describe('<DeleteShortUrlModal />', () => {
@ -11,7 +10,7 @@ describe('<DeleteShortUrlModal />', () => {
shortCode: 'abc123', shortCode: 'abc123',
originalUrl: 'https://long-domain.com/foo/bar', originalUrl: 'https://long-domain.com/foo/bar',
}; };
const deleteShortUrl = sinon.fake.returns(Promise.resolve()); const deleteShortUrl = jest.fn(() => Promise.resolve());
const createWrapper = (shortUrlDeletion) => { const createWrapper = (shortUrlDeletion) => {
wrapper = shallow( wrapper = shallow(
<DeleteShortUrlModal <DeleteShortUrlModal
@ -30,7 +29,7 @@ describe('<DeleteShortUrlModal />', () => {
afterEach(() => { afterEach(() => {
wrapper && wrapper.unmount(); wrapper && wrapper.unmount();
deleteShortUrl.resetHistory(); deleteShortUrl.mockClear();
}); });
it('shows threshold error message when threshold error occurs', () => { it('shows threshold error message when threshold error occurs', () => {
@ -106,9 +105,9 @@ describe('<DeleteShortUrlModal />', () => {
setImmediate(() => { setImmediate(() => {
const form = wrapper.find('form'); const form = wrapper.find('form');
expect(deleteShortUrl.callCount).toEqual(0); expect(deleteShortUrl).not.toHaveBeenCalled();
form.simulate('submit', { preventDefault: identity }); form.simulate('submit', { preventDefault: identity });
expect(deleteShortUrl.callCount).toEqual(1); expect(deleteShortUrl).toHaveBeenCalledTimes(1);
done(); done();
}); });
}); });

View file

@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import { Modal } from 'reactstrap'; import { Modal } from 'reactstrap';
import createEditTagsModal from '../../../src/short-urls/helpers/EditTagsModal'; import createEditTagsModal from '../../../src/short-urls/helpers/EditTagsModal';
@ -8,10 +7,10 @@ describe('<EditTagsModal />', () => {
let wrapper; let wrapper;
const shortCode = 'abc123'; const shortCode = 'abc123';
const TagsSelector = () => ''; const TagsSelector = () => '';
const editShortUrlTags = sinon.fake.resolves(); const editShortUrlTags = jest.fn(() => Promise.resolve());
const shortUrlTagsEdited = sinon.fake(); const shortUrlTagsEdited = jest.fn();
const resetShortUrlsTags = sinon.fake(); const resetShortUrlsTags = jest.fn();
const toggle = sinon.fake(); const toggle = jest.fn();
const createWrapper = (shortUrlTags) => { const createWrapper = (shortUrlTags) => {
const EditTagsModal = createEditTagsModal(TagsSelector); const EditTagsModal = createEditTagsModal(TagsSelector);
@ -37,10 +36,10 @@ describe('<EditTagsModal />', () => {
afterEach(() => { afterEach(() => {
wrapper && wrapper.unmount(); wrapper && wrapper.unmount();
editShortUrlTags.resetHistory(); editShortUrlTags.mockClear();
shortUrlTagsEdited.resetHistory(); shortUrlTagsEdited.mockReset();
resetShortUrlsTags.resetHistory(); resetShortUrlsTags.mockReset();
toggle.resetHistory(); toggle.mockReset();
}); });
it('resets tags when component is mounted', () => { it('resets tags when component is mounted', () => {
@ -51,7 +50,7 @@ describe('<EditTagsModal />', () => {
error: false, error: false,
}); });
expect(resetShortUrlsTags.callCount).toEqual(1); expect(resetShortUrlsTags).toHaveBeenCalledTimes(1);
}); });
it('renders tags selector and save button when loaded', () => { it('renders tags selector and save button when loaded', () => {
@ -92,12 +91,12 @@ describe('<EditTagsModal />', () => {
saveBtn.simulate('click'); saveBtn.simulate('click');
expect(editShortUrlTags.callCount).toEqual(1); expect(editShortUrlTags).toHaveBeenCalledTimes(1);
expect(editShortUrlTags.getCall(0).args).toEqual([ shortCode, []]); expect(editShortUrlTags).toHaveBeenCalledWith(shortCode, []);
// Wrap this expect in a setImmediate since it is called as a result of an inner promise // Wrap this expect in a setImmediate since it is called as a result of an inner promise
setImmediate(() => { setImmediate(() => {
expect(toggle.callCount).toEqual(1); expect(toggle).toHaveBeenCalledTimes(1);
done(); done();
}); });
}); });
@ -112,7 +111,7 @@ describe('<EditTagsModal />', () => {
const modal = wrapper.find(Modal); const modal = wrapper.find(Modal);
modal.simulate('closed'); modal.simulate('closed');
expect(shortUrlTagsEdited.callCount).toEqual(0); expect(shortUrlTagsEdited).not.toHaveBeenCalled();
}); });
it('notifies tags have been edited when window is closed after saving', (done) => { it('notifies tags have been edited when window is closed after saving', (done) => {
@ -130,8 +129,8 @@ describe('<EditTagsModal />', () => {
// Wrap this expect in a setImmediate since it is called as a result of an inner promise // Wrap this expect in a setImmediate since it is called as a result of an inner promise
setImmediate(() => { setImmediate(() => {
modal.simulate('closed'); modal.simulate('closed');
expect(shortUrlTagsEdited.callCount).toEqual(1); expect(shortUrlTagsEdited).toHaveBeenCalledTimes(1);
expect(shortUrlTagsEdited.getCall(0).args).toEqual([ shortCode, []]); expect(shortUrlTagsEdited).toHaveBeenCalledWith(shortCode, []);
done(); done();
}); });
}); });
@ -146,6 +145,6 @@ describe('<EditTagsModal />', () => {
const cancelBtn = wrapper.find('.btn-link'); const cancelBtn = wrapper.find('.btn-link');
cancelBtn.simulate('click'); cancelBtn.simulate('click');
expect(toggle.callCount).toEqual(1); expect(toggle).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -3,7 +3,6 @@ import { shallow } from 'enzyme';
import moment from 'moment'; import moment from 'moment';
import Moment from 'react-moment'; import Moment from 'react-moment';
import { assoc, toString } from 'ramda'; import { assoc, toString } from 'ramda';
import * as sinon from 'sinon';
import createShortUrlsRow from '../../../src/short-urls/helpers/ShortUrlsRow'; import createShortUrlsRow from '../../../src/short-urls/helpers/ShortUrlsRow';
import ExternalLink from '../../../src/utils/ExternalLink'; import ExternalLink from '../../../src/utils/ExternalLink';
import Tag from '../../../src/tags/helpers/Tag'; import Tag from '../../../src/tags/helpers/Tag';
@ -12,7 +11,7 @@ describe('<ShortUrlsRow />', () => {
let wrapper; let wrapper;
const mockFunction = () => ''; const mockFunction = () => '';
const ShortUrlsRowMenu = mockFunction; const ShortUrlsRowMenu = mockFunction;
const stateFlagTimeout = sinon.spy(); const stateFlagTimeout = jest.fn();
const colorGenerator = { const colorGenerator = {
getColorForKey: mockFunction, getColorForKey: mockFunction,
setColorForKey: mockFunction, setColorForKey: mockFunction,
@ -92,9 +91,9 @@ describe('<ShortUrlsRow />', () => {
const menu = col.find(ShortUrlsRowMenu); const menu = col.find(ShortUrlsRowMenu);
expect(menu).toHaveLength(1); expect(menu).toHaveLength(1);
expect(stateFlagTimeout.called).toEqual(false); expect(stateFlagTimeout).not.toHaveBeenCalled();
menu.simulate('copyToClipboard'); menu.simulate('copyToClipboard');
expect(stateFlagTimeout.calledOnce).toEqual(true); expect(stateFlagTimeout).toHaveBeenCalledTimes(1);
}); });
it('shows copy hint when state prop is true', () => { it('shows copy hint when state prop is true', () => {

View file

@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import { ButtonDropdown, DropdownItem } from 'reactstrap'; import { ButtonDropdown, DropdownItem } from 'reactstrap';
import createShortUrlsRowMenu from '../../../src/short-urls/helpers/ShortUrlsRowMenu'; import createShortUrlsRowMenu from '../../../src/short-urls/helpers/ShortUrlsRowMenu';
import PreviewModal from '../../../src/short-urls/helpers/PreviewModal'; import PreviewModal from '../../../src/short-urls/helpers/PreviewModal';
@ -10,7 +9,7 @@ describe('<ShortUrlsRowMenu />', () => {
let wrapper; let wrapper;
const DeleteShortUrlModal = () => ''; const DeleteShortUrlModal = () => '';
const EditTagsModal = () => ''; const EditTagsModal = () => '';
const onCopyToClipboard = sinon.spy(); const onCopyToClipboard = jest.fn();
const selectedServer = { id: 'abc123' }; const selectedServer = { id: 'abc123' };
const shortUrl = { const shortUrl = {
shortCode: 'abc123', shortCode: 'abc123',

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
CREATE_SHORT_URL_START, CREATE_SHORT_URL_START,
CREATE_SHORT_URL_ERROR, CREATE_SHORT_URL_ERROR,
@ -48,30 +47,27 @@ describe('shortUrlCreationReducer', () => {
describe('createShortUrl', () => { describe('createShortUrl', () => {
const createApiClientMock = (result) => ({ const createApiClientMock = (result) => ({
createShortUrl: sinon.fake.returns(result), createShortUrl: jest.fn(() => result),
}); });
const dispatch = sinon.spy(); const dispatch = jest.fn();
const getState = () => ({}); const getState = () => ({});
afterEach(() => dispatch.resetHistory()); afterEach(() => dispatch.mockReset());
it('calls API on success', async () => { it('calls API on success', async () => {
const expectedDispatchCalls = 2;
const result = 'foo'; const result = 'foo';
const apiClientMock = createApiClientMock(Promise.resolve(result)); const apiClientMock = createApiClientMock(Promise.resolve(result));
const dispatchable = createShortUrl(() => apiClientMock)({}); const dispatchable = createShortUrl(() => apiClientMock)({});
await dispatchable(dispatch, getState); await dispatchable(dispatch, getState);
expect(apiClientMock.createShortUrl.callCount).toEqual(1); expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
expect(dispatch.getCall(0).args).toEqual([{ type: CREATE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result });
expect(dispatch.getCall(1).args).toEqual([{ type: CREATE_SHORT_URL, result }]);
}); });
it('throws on error', async () => { it('throws on error', async () => {
const expectedDispatchCalls = 2;
const error = 'Error'; const error = 'Error';
const apiClientMock = createApiClientMock(Promise.reject(error)); const apiClientMock = createApiClientMock(Promise.reject(error));
const dispatchable = createShortUrl(() => apiClientMock)({}); const dispatchable = createShortUrl(() => apiClientMock)({});
@ -82,11 +78,10 @@ describe('shortUrlCreationReducer', () => {
expect(e).toEqual(error); expect(e).toEqual(error);
} }
expect(apiClientMock.createShortUrl.callCount).toEqual(1); expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
expect(dispatch.getCall(0).args).toEqual([{ type: CREATE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL_ERROR });
expect(dispatch.getCall(1).args).toEqual([{ type: CREATE_SHORT_URL_ERROR }]);
}); });
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
DELETE_SHORT_URL, DELETE_SHORT_URL_ERROR, DELETE_SHORT_URL, DELETE_SHORT_URL_ERROR,
DELETE_SHORT_URL_START, DELETE_SHORT_URL_START,
@ -58,39 +57,37 @@ describe('shortUrlDeletionReducer', () => {
}); });
describe('deleteShortUrl', () => { describe('deleteShortUrl', () => {
const dispatch = sinon.spy(); const dispatch = jest.fn();
const getState = sinon.fake.returns({ selectedServer: {} }); const getState = jest.fn().mockReturnValue({ selectedServer: {} });
afterEach(() => { afterEach(() => {
dispatch.resetHistory(); dispatch.mockReset();
getState.resetHistory(); getState.mockClear();
}); });
it('dispatches proper actions if API client request succeeds', async () => { it('dispatches proper actions if API client request succeeds', async () => {
const apiClientMock = { const apiClientMock = {
deleteShortUrl: sinon.fake.resolves(''), deleteShortUrl: jest.fn(() => ''),
}; };
const shortCode = 'abc123'; const shortCode = 'abc123';
const expectedDispatchCalls = 2;
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState); await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_SHORT_URL, shortCode }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL, shortCode });
expect(apiClientMock.deleteShortUrl.callCount).toEqual(1); expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteShortUrl.getCall(0).args).toEqual([ shortCode ]); expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode);
}); });
it('dispatches proper actions if API client request fails', async () => { it('dispatches proper actions if API client request fails', async () => {
const data = { foo: 'bar' }; const data = { foo: 'bar' };
const error = { response: { data } }; const error = { response: { data } };
const apiClientMock = { const apiClientMock = {
deleteShortUrl: sinon.fake.returns(Promise.reject(error)), deleteShortUrl: jest.fn(() => Promise.reject(error)),
}; };
const shortCode = 'abc123'; const shortCode = 'abc123';
const expectedDispatchCalls = 2;
try { try {
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState); await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
@ -98,12 +95,12 @@ describe('shortUrlDeletionReducer', () => {
expect(e).toEqual(error); expect(e).toEqual(error);
} }
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_SHORT_URL_ERROR, errorData: data }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL_ERROR, errorData: data });
expect(apiClientMock.deleteShortUrl.callCount).toEqual(1); expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteShortUrl.getCall(0).args).toEqual([ shortCode ]); expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode);
}); });
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
LIST_SHORT_URLS, LIST_SHORT_URLS,
LIST_SHORT_URLS_ERROR, LIST_SHORT_URLS_ERROR,
@ -73,42 +72,40 @@ describe('shortUrlsListReducer', () => {
}); });
describe('listShortUrls', () => { describe('listShortUrls', () => {
const dispatch = sinon.spy(); const dispatch = jest.fn();
const getState = sinon.fake.returns({ selectedServer: {} }); const getState = jest.fn().mockReturnValue({ selectedServer: {} });
afterEach(() => { afterEach(() => {
dispatch.resetHistory(); dispatch.mockReset();
getState.resetHistory(); getState.mockClear();
}); });
it('dispatches proper actions if API client request succeeds', async () => { it('dispatches proper actions if API client request succeeds', async () => {
const apiClientMock = { const apiClientMock = {
listShortUrls: sinon.fake.resolves([]), listShortUrls: jest.fn().mockResolvedValue([]),
}; };
const expectedDispatchCalls = 2;
await listShortUrls(() => apiClientMock)()(dispatch, getState); await listShortUrls(() => apiClientMock)()(dispatch, getState);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: LIST_SHORT_URLS_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
expect(dispatch.getCall(1).args).toEqual([{ type: LIST_SHORT_URLS, shortUrls: [], params: {} }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS, shortUrls: [], params: {} });
expect(apiClientMock.listShortUrls.callCount).toEqual(1); expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
}); });
it('dispatches proper actions if API client request fails', async () => { it('dispatches proper actions if API client request fails', async () => {
const apiClientMock = { const apiClientMock = {
listShortUrls: sinon.fake.rejects(), listShortUrls: jest.fn().mockRejectedValue(),
}; };
const expectedDispatchCalls = 2;
await listShortUrls(() => apiClientMock)()(dispatch, getState); await listShortUrls(() => apiClientMock)()(dispatch, getState);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: LIST_SHORT_URLS_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
expect(dispatch.getCall(1).args).toEqual([{ type: LIST_SHORT_URLS_ERROR, params: {} }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS_ERROR, params: {} });
expect(apiClientMock.listShortUrls.callCount).toEqual(1); expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
}); });
}); });
}); });

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import * as sinon from 'sinon';
import createTagsList from '../../src/tags/TagsList'; import createTagsList from '../../src/tags/TagsList';
import MuttedMessage from '../../src/utils/MuttedMessage'; import MuttedMessage from '../../src/utils/MuttedMessage';
import SearchField from '../../src/utils/SearchField'; import SearchField from '../../src/utils/SearchField';
@ -9,7 +8,7 @@ import { rangeOf } from '../../src/utils/utils';
describe('<TagsList />', () => { describe('<TagsList />', () => {
let wrapper; let wrapper;
const filterTags = sinon.spy(); const filterTags = jest.fn();
const TagCard = () => ''; const TagCard = () => '';
const createWrapper = (tagsList) => { const createWrapper = (tagsList) => {
const params = { serverId: '1' }; const params = { serverId: '1' };
@ -24,7 +23,7 @@ describe('<TagsList />', () => {
afterEach(() => { afterEach(() => {
wrapper && wrapper.unmount(); wrapper && wrapper.unmount();
filterTags.resetHistory(); filterTags.mockReset();
}); });
it('shows a loading message when tags are being loaded', () => { it('shows a loading message when tags are being loaded', () => {
@ -67,11 +66,11 @@ describe('<TagsList />', () => {
const searchField = wrapper.find(SearchField); const searchField = wrapper.find(SearchField);
expect(searchField).toHaveLength(1); expect(searchField).toHaveLength(1);
expect(filterTags.callCount).toEqual(0); expect(filterTags).not.toHaveBeenCalled();
searchField.simulate('change'); searchField.simulate('change');
setImmediate(() => { setImmediate(() => {
expect(filterTags.callCount).toEqual(1); expect(filterTags).toHaveBeenCalledTimes(1);
done(); done();
}); });
}); });

View file

@ -1,14 +1,13 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import { Modal, ModalBody, ModalFooter } from 'reactstrap'; import { Modal, ModalBody, ModalFooter } from 'reactstrap';
import DeleteTagConfirmModal from '../../../src/tags/helpers/DeleteTagConfirmModal'; import DeleteTagConfirmModal from '../../../src/tags/helpers/DeleteTagConfirmModal';
describe('<DeleteTagConfirmModal />', () => { describe('<DeleteTagConfirmModal />', () => {
let wrapper; let wrapper;
const tag = 'nodejs'; const tag = 'nodejs';
const deleteTag = sinon.spy(); const deleteTag = jest.fn();
const tagDeleted = sinon.spy(); const tagDeleted = jest.fn();
const createWrapper = (tagDelete) => { const createWrapper = (tagDelete) => {
wrapper = shallow( wrapper = shallow(
<DeleteTagConfirmModal <DeleteTagConfirmModal
@ -26,8 +25,8 @@ describe('<DeleteTagConfirmModal />', () => {
afterEach(() => { afterEach(() => {
wrapper && wrapper.unmount(); wrapper && wrapper.unmount();
deleteTag.resetHistory(); deleteTag.mockReset();
tagDeleted.resetHistory(); tagDeleted.mockReset();
}); });
it('asks confirmation for provided tag to be deleted', () => { it('asks confirmation for provided tag to be deleted', () => {
@ -63,8 +62,8 @@ describe('<DeleteTagConfirmModal />', () => {
const delBtn = footer.find('.btn-danger'); const delBtn = footer.find('.btn-danger');
delBtn.simulate('click'); delBtn.simulate('click');
expect(deleteTag.calledOnce).toEqual(true); expect(deleteTag).toHaveBeenCalledTimes(1);
expect(deleteTag.calledWith(tag)).toEqual(true); expect(deleteTag).toHaveBeenCalledWith(tag);
}); });
it('does no further actions when modal is closed without deleting tag', () => { it('does no further actions when modal is closed without deleting tag', () => {
@ -72,7 +71,7 @@ describe('<DeleteTagConfirmModal />', () => {
const modal = wrapper.find(Modal); const modal = wrapper.find(Modal);
modal.simulate('closed'); modal.simulate('closed');
expect(tagDeleted.called).toEqual(false); expect(tagDeleted).not.toHaveBeenCalled();
}); });
it('notifies tag to be deleted when modal is closed after deleting tag', () => { it('notifies tag to be deleted when modal is closed after deleting tag', () => {
@ -81,7 +80,7 @@ describe('<DeleteTagConfirmModal />', () => {
wrapper.instance().tagWasDeleted = true; wrapper.instance().tagWasDeleted = true;
modal.simulate('closed'); modal.simulate('closed');
expect(tagDeleted.calledOnce).toEqual(true); expect(tagDeleted).toHaveBeenCalledTimes(1);
expect(tagDeleted.calledWith(tag)).toEqual(true); expect(tagDeleted).toHaveBeenCalledWith(tag);
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
DELETE_TAG_START, DELETE_TAG_START,
DELETE_TAG_ERROR, DELETE_TAG_ERROR,
@ -42,31 +41,29 @@ describe('tagDeleteReducer', () => {
describe('deleteTag', () => { describe('deleteTag', () => {
const createApiClientMock = (result) => ({ const createApiClientMock = (result) => ({
deleteTags: sinon.fake.returns(result), deleteTags: jest.fn(() => result),
}); });
const dispatch = sinon.spy(); const dispatch = jest.fn();
const getState = () => ({}); const getState = () => ({});
afterEach(() => dispatch.resetHistory()); afterEach(() => dispatch.mockReset());
it('calls API on success', async () => { it('calls API on success', async () => {
const expectedDispatchCalls = 2;
const tag = 'foo'; const tag = 'foo';
const apiClientMock = createApiClientMock(Promise.resolve()); const apiClientMock = createApiClientMock(Promise.resolve());
const dispatchable = deleteTag(() => apiClientMock)(tag); const dispatchable = deleteTag(() => apiClientMock)(tag);
await dispatchable(dispatch, getState); await dispatchable(dispatch, getState);
expect(apiClientMock.deleteTags.callCount).toEqual(1); expect(apiClientMock.deleteTags).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteTags.getCall(0).args).toEqual([[ tag ]]); expect(apiClientMock.deleteTags).toHaveBeenNthCalledWith(1, [ tag ]);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_TAG_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_TAG_START });
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_TAG }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_TAG });
}); });
it('throws on error', async () => { it('throws on error', async () => {
const expectedDispatchCalls = 2;
const error = 'Error'; const error = 'Error';
const tag = 'foo'; const tag = 'foo';
const apiClientMock = createApiClientMock(Promise.reject(error)); const apiClientMock = createApiClientMock(Promise.reject(error));
@ -78,12 +75,12 @@ describe('tagDeleteReducer', () => {
expect(e).toEqual(error); expect(e).toEqual(error);
} }
expect(apiClientMock.deleteTags.callCount).toEqual(1); expect(apiClientMock.deleteTags).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteTags.getCall(0).args).toEqual([[ tag ]]); expect(apiClientMock.deleteTags).toHaveBeenNthCalledWith(1, [ tag ]);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_TAG_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_TAG_START });
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_TAG_ERROR }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_TAG_ERROR });
}); });
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
EDIT_TAG_START, EDIT_TAG_START,
EDIT_TAG_ERROR, EDIT_TAG_ERROR,
@ -46,21 +45,20 @@ describe('tagEditReducer', () => {
describe('editTag', () => { describe('editTag', () => {
const createApiClientMock = (result) => ({ const createApiClientMock = (result) => ({
editTag: sinon.fake.returns(result), editTag: jest.fn(() => result),
}); });
const colorGenerator = { const colorGenerator = {
setColorForKey: sinon.spy(), setColorForKey: jest.fn(),
}; };
const dispatch = sinon.spy(); const dispatch = jest.fn();
const getState = () => ({}); const getState = () => ({});
afterEach(() => { afterEach(() => {
colorGenerator.setColorForKey.resetHistory(); colorGenerator.setColorForKey.mockReset();
dispatch.resetHistory(); dispatch.mockReset();
}); });
it('calls API on success', async () => { it('calls API on success', async () => {
const expectedDispatchCalls = 2;
const oldName = 'foo'; const oldName = 'foo';
const newName = 'bar'; const newName = 'bar';
const color = '#ff0000'; const color = '#ff0000';
@ -69,19 +67,18 @@ describe('tagEditReducer', () => {
await dispatchable(dispatch, getState); await dispatchable(dispatch, getState);
expect(apiClientMock.editTag.callCount).toEqual(1); expect(apiClientMock.editTag).toHaveBeenCalledTimes(1);
expect(apiClientMock.editTag.getCall(0).args).toEqual([ oldName, newName ]); expect(apiClientMock.editTag).toHaveBeenCalledWith(oldName, newName);
expect(colorGenerator.setColorForKey.callCount).toEqual(1); expect(colorGenerator.setColorForKey).toHaveBeenCalledTimes(1);
expect(colorGenerator.setColorForKey.getCall(0).args).toEqual([ newName, color ]); expect(colorGenerator.setColorForKey).toHaveBeenCalledWith(newName, color);
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: EDIT_TAG_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_TAG_START });
expect(dispatch.getCall(1).args).toEqual([{ type: EDIT_TAG, oldName, newName }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_TAG, oldName, newName });
}); });
it('throws on error', async () => { it('throws on error', async () => {
const expectedDispatchCalls = 2;
const error = 'Error'; const error = 'Error';
const oldName = 'foo'; const oldName = 'foo';
const newName = 'bar'; const newName = 'bar';
@ -95,14 +92,14 @@ describe('tagEditReducer', () => {
expect(e).toEqual(error); expect(e).toEqual(error);
} }
expect(apiClientMock.editTag.callCount).toEqual(1); expect(apiClientMock.editTag).toHaveBeenCalledTimes(1);
expect(apiClientMock.editTag.getCall(0).args).toEqual([ oldName, newName ]); expect(apiClientMock.editTag).toHaveBeenCalledWith(oldName, newName);
expect(colorGenerator.setColorForKey.callCount).toEqual(0); expect(colorGenerator.setColorForKey).not.toHaveBeenCalled();
expect(dispatch.callCount).toEqual(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.getCall(0).args).toEqual([{ type: EDIT_TAG_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_TAG_START });
expect(dispatch.getCall(1).args).toEqual([{ type: EDIT_TAG_ERROR }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_TAG_ERROR });
}); });
}); });
}); });

View file

@ -4,7 +4,6 @@ import { DropdownItem } from 'reactstrap';
import { identity, values } from 'ramda'; import { identity, values } from 'ramda';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSortAmountDown as caretDownIcon } from '@fortawesome/free-solid-svg-icons'; import { faSortAmountDown as caretDownIcon } from '@fortawesome/free-solid-svg-icons';
import * as sinon from 'sinon';
import SortingDropdown from '../../src/utils/SortingDropdown'; import SortingDropdown from '../../src/utils/SortingDropdown';
describe('<SortingDropdown />', () => { describe('<SortingDropdown />', () => {
@ -44,35 +43,35 @@ describe('<SortingDropdown />', () => {
}); });
it('triggers change function when item is clicked and no order field was provided', () => { it('triggers change function when item is clicked and no order field was provided', () => {
const onChange = sinon.spy(); const onChange = jest.fn();
const wrapper = createWrapper({ onChange }); const wrapper = createWrapper({ onChange });
const firstItem = wrapper.find(DropdownItem).first(); const firstItem = wrapper.find(DropdownItem).first();
firstItem.simulate('click'); firstItem.simulate('click');
expect(onChange.callCount).toEqual(1); expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.calledWith('foo', 'ASC')).toEqual(true); expect(onChange).toHaveBeenCalledWith('foo', 'ASC');
}); });
it('triggers change function when item is clicked and an order field was provided', () => { it('triggers change function when item is clicked and an order field was provided', () => {
const onChange = sinon.spy(); const onChange = jest.fn();
const wrapper = createWrapper({ onChange, orderField: 'baz', orderDir: 'ASC' }); const wrapper = createWrapper({ onChange, orderField: 'baz', orderDir: 'ASC' });
const firstItem = wrapper.find(DropdownItem).first(); const firstItem = wrapper.find(DropdownItem).first();
firstItem.simulate('click'); firstItem.simulate('click');
expect(onChange.callCount).toEqual(1); expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.calledWith('foo', 'ASC')).toEqual(true); expect(onChange).toHaveBeenCalledWith('foo', 'ASC');
}); });
it('updates order dir when already selected item is clicked', () => { it('updates order dir when already selected item is clicked', () => {
const onChange = sinon.spy(); const onChange = jest.fn();
const wrapper = createWrapper({ onChange, orderField: 'foo', orderDir: 'ASC' }); const wrapper = createWrapper({ onChange, orderField: 'foo', orderDir: 'ASC' });
const firstItem = wrapper.find(DropdownItem).first(); const firstItem = wrapper.find(DropdownItem).first();
firstItem.simulate('click'); firstItem.simulate('click');
expect(onChange.callCount).toEqual(1); expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.calledWith('foo', 'DESC')).toEqual(true); expect(onChange).toHaveBeenCalledWith('foo', 'DESC');
}); });
}); });

View file

@ -1,16 +1,15 @@
import * as sinon from 'sinon';
import ColorGenerator from '../../../src/utils/services/ColorGenerator'; import ColorGenerator from '../../../src/utils/services/ColorGenerator';
describe('ColorGenerator', () => { describe('ColorGenerator', () => {
let colorGenerator; let colorGenerator;
const storageMock = { const storageMock = {
set: sinon.fake(), set: jest.fn(),
get: sinon.fake.returns(undefined), get: jest.fn(),
}; };
beforeEach(() => { beforeEach(() => {
storageMock.set.resetHistory(); storageMock.set.mockReset();
storageMock.get.resetHistory(); storageMock.get.mockReset();
colorGenerator = new ColorGenerator(storageMock); colorGenerator = new ColorGenerator(storageMock);
}); });
@ -21,14 +20,14 @@ describe('ColorGenerator', () => {
colorGenerator.setColorForKey('foo', color); colorGenerator.setColorForKey('foo', color);
expect(colorGenerator.getColorForKey('foo')).toEqual(color); expect(colorGenerator.getColorForKey('foo')).toEqual(color);
expect(storageMock.set.callCount).toEqual(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
}); });
it('generates a random color when none is available for requested key', () => { it('generates a random color when none is available for requested key', () => {
expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/)); expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/));
expect(storageMock.set.callCount).toEqual(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
}); });
it('trims and lower cases keys before trying to match', () => { it('trims and lower cases keys before trying to match', () => {
@ -42,7 +41,7 @@ describe('ColorGenerator', () => {
expect(colorGenerator.getColorForKey('FOO')).toEqual(color); expect(colorGenerator.getColorForKey('FOO')).toEqual(color);
expect(colorGenerator.getColorForKey('FOO ')).toEqual(color); expect(colorGenerator.getColorForKey('FOO ')).toEqual(color);
expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color); expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color);
expect(storageMock.set.callCount).toEqual(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(storageMock.get.callCount).toEqual(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
}); });
}); });

View file

@ -1,5 +1,3 @@
import sinon from 'sinon';
import { head, last } from 'ramda';
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient'; import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
describe('ShlinkApiClient', () => { describe('ShlinkApiClient', () => {
@ -35,23 +33,21 @@ describe('ShlinkApiClient', () => {
}); });
it('removes all empty options', async () => { it('removes all empty options', async () => {
const axiosSpy = sinon.spy(createAxiosMock({ data: shortUrl })); const axiosSpy = jest.fn(createAxiosMock({ data: shortUrl }));
const { createShortUrl } = new ShlinkApiClient(axiosSpy); const { createShortUrl } = new ShlinkApiClient(axiosSpy);
await createShortUrl( await createShortUrl(
{ foo: 'bar', empty: undefined, anotherEmpty: null } { foo: 'bar', empty: undefined, anotherEmpty: null }
); );
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(axiosArgs.data).toEqual({ foo: 'bar' }); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ data: { foo: 'bar' } }));
}); });
}); });
describe('getShortUrlVisits', () => { describe('getShortUrlVisits', () => {
it('properly returns short URL visits', async () => { it('properly returns short URL visits', async () => {
const expectedVisits = [ 'foo', 'bar' ]; const expectedVisits = [ 'foo', 'bar' ];
const axiosSpy = sinon.spy(createAxiosMock({ const axiosSpy = jest.fn(createAxiosMock({
data: { data: {
visits: { visits: {
data: expectedVisits, data: expectedVisits,
@ -61,55 +57,55 @@ describe('ShlinkApiClient', () => {
const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy); const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy);
const actualVisits = await getShortUrlVisits('abc123', {}); const actualVisits = await getShortUrlVisits('abc123', {});
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect({ data: expectedVisits }).toEqual(actualVisits); expect({ data: expectedVisits }).toEqual(actualVisits);
expect(axiosArgs.url).toContain('/short-urls/abc123/visits'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(axiosArgs.method).toEqual('GET'); url: '/short-urls/abc123/visits',
method: 'GET',
}));
}); });
}); });
describe('getShortUrl', () => { describe('getShortUrl', () => {
it('properly returns short URL', async () => { it('properly returns short URL', async () => {
const expectedShortUrl = { foo: 'bar' }; const expectedShortUrl = { foo: 'bar' };
const axiosSpy = sinon.spy(createAxiosMock({ const axiosSpy = jest.fn(createAxiosMock({
data: expectedShortUrl, data: expectedShortUrl,
})); }));
const { getShortUrl } = new ShlinkApiClient(axiosSpy); const { getShortUrl } = new ShlinkApiClient(axiosSpy);
const result = await getShortUrl('abc123'); const result = await getShortUrl('abc123');
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(expectedShortUrl).toEqual(result); expect(expectedShortUrl).toEqual(result);
expect(axiosArgs.url).toContain('/short-urls/abc123'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(axiosArgs.method).toEqual('GET'); url: '/short-urls/abc123',
method: 'GET',
}));
}); });
}); });
describe('updateShortUrlTags', () => { describe('updateShortUrlTags', () => {
it('properly updates short URL tags', async () => { it('properly updates short URL tags', async () => {
const expectedTags = [ 'foo', 'bar' ]; const expectedTags = [ 'foo', 'bar' ];
const axiosSpy = sinon.spy(createAxiosMock({ const axiosSpy = jest.fn(createAxiosMock({
data: { tags: expectedTags }, data: { tags: expectedTags },
})); }));
const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy); const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy);
const result = await updateShortUrlTags('abc123', expectedTags); const result = await updateShortUrlTags('abc123', expectedTags);
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(expectedTags).toEqual(result); expect(expectedTags).toEqual(result);
expect(axiosArgs.url).toContain('/short-urls/abc123/tags'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(axiosArgs.method).toEqual('PUT'); url: '/short-urls/abc123/tags',
method: 'PUT',
}));
}); });
}); });
describe('listTags', () => { describe('listTags', () => {
it('properly returns list of tags', async () => { it('properly returns list of tags', async () => {
const expectedTags = [ 'foo', 'bar' ]; const expectedTags = [ 'foo', 'bar' ];
const axiosSpy = sinon.spy(createAxiosMock({ const axiosSpy = jest.fn(createAxiosMock({
data: { data: {
tags: { data: expectedTags }, tags: { data: expectedTags },
}, },
@ -117,28 +113,25 @@ describe('ShlinkApiClient', () => {
const { listTags } = new ShlinkApiClient(axiosSpy); const { listTags } = new ShlinkApiClient(axiosSpy);
const result = await listTags(); const result = await listTags();
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(expectedTags).toEqual(result); expect(expectedTags).toEqual(result);
expect(axiosArgs.url).toContain('/tags'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ url: '/tags', method: 'GET' }));
expect(axiosArgs.method).toEqual('GET');
}); });
}); });
describe('deleteTags', () => { describe('deleteTags', () => {
it('properly deletes provided tags', async () => { it('properly deletes provided tags', async () => {
const tags = [ 'foo', 'bar' ]; const tags = [ 'foo', 'bar' ];
const axiosSpy = sinon.spy(createAxiosMock({})); const axiosSpy = jest.fn(createAxiosMock({}));
const { deleteTags } = new ShlinkApiClient(axiosSpy); const { deleteTags } = new ShlinkApiClient(axiosSpy);
await deleteTags(tags); await deleteTags(tags);
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(axiosArgs.url).toContain('/tags'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(axiosArgs.method).toEqual('DELETE'); url: '/tags',
expect(axiosArgs.params).toEqual({ tags }); method: 'DELETE',
params: { tags },
}));
}); });
}); });
@ -146,30 +139,30 @@ describe('ShlinkApiClient', () => {
it('properly edits provided tag', async () => { it('properly edits provided tag', async () => {
const oldName = 'foo'; const oldName = 'foo';
const newName = 'bar'; const newName = 'bar';
const axiosSpy = sinon.spy(createAxiosMock({})); const axiosSpy = jest.fn(createAxiosMock({}));
const { editTag } = new ShlinkApiClient(axiosSpy); const { editTag } = new ShlinkApiClient(axiosSpy);
await editTag(oldName, newName); await editTag(oldName, newName);
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(axiosArgs.url).toContain('/tags'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(axiosArgs.method).toEqual('PUT'); url: '/tags',
expect(axiosArgs.data).toEqual({ oldName, newName }); method: 'PUT',
data: { oldName, newName },
}));
}); });
}); });
describe('deleteShortUrl', () => { describe('deleteShortUrl', () => {
it('properly deletes provided short URL', async () => { it('properly deletes provided short URL', async () => {
const axiosSpy = sinon.spy(createAxiosMock({})); const axiosSpy = jest.fn(createAxiosMock({}));
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy); const { deleteShortUrl } = new ShlinkApiClient(axiosSpy);
await deleteShortUrl('abc123'); await deleteShortUrl('abc123');
const lastAxiosCall = last(axiosSpy.getCalls());
const axiosArgs = head(lastAxiosCall.args);
expect(axiosArgs.url).toContain('/short-urls/abc123'); expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
expect(axiosArgs.method).toEqual('DELETE'); url: '/short-urls/abc123',
method: 'DELETE',
}));
}); });
}); });
}); });

View file

@ -1,16 +1,15 @@
import * as sinon from 'sinon';
import Storage from '../../../src/utils/services/Storage'; import Storage from '../../../src/utils/services/Storage';
describe('Storage', () => { describe('Storage', () => {
const localStorageMock = { const localStorageMock = {
getItem: sinon.fake((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null), getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
setItem: sinon.spy(), setItem: jest.fn(),
}; };
let storage; let storage;
beforeEach(() => { beforeEach(() => {
localStorageMock.getItem.resetHistory(); localStorageMock.getItem.mockClear();
localStorageMock.setItem.resetHistory(); localStorageMock.setItem.mockReset();
storage = new Storage(localStorageMock); storage = new Storage(localStorageMock);
}); });
@ -21,18 +20,15 @@ describe('Storage', () => {
storage.set('foo', value); storage.set('foo', value);
expect(localStorageMock.setItem.callCount).toEqual(1); expect(localStorageMock.setItem).toHaveBeenCalledTimes(1);
expect(localStorageMock.setItem.getCall(0).args).toEqual([ expect(localStorageMock.setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
'shlink.foo',
JSON.stringify(value),
]);
}); });
}); });
describe('get', () => { describe('get', () => {
it('fetches item from local storage', () => { it('fetches item from local storage', () => {
storage.get('foo'); storage.get('foo');
expect(localStorageMock.getItem.callCount).toEqual(1); expect(localStorageMock.getItem).toHaveBeenCalledTimes(1);
}); });
it('returns parsed value when requested value is found in local storage', () => { it('returns parsed value when requested value is found in local storage', () => {

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import L from 'leaflet'; import L from 'leaflet';
import marker2x from 'leaflet/dist/images/marker-icon-2x.png'; import marker2x from 'leaflet/dist/images/marker-icon-2x.png';
import marker from 'leaflet/dist/images/marker-icon.png'; import marker from 'leaflet/dist/images/marker-icon.png';
@ -14,19 +13,18 @@ import {
describe('utils', () => { describe('utils', () => {
describe('stateFlagTimeout', () => { describe('stateFlagTimeout', () => {
it('sets state and initializes timeout with provided delay', () => { it('sets state and initializes timeout with provided delay', () => {
const setTimeout = sinon.fake((callback) => callback()); const setTimeout = jest.fn((callback) => callback());
const setState = sinon.spy(); const setState = jest.fn();
const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout); const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout);
const delay = 5000; const delay = 5000;
const expectedSetStateCalls = 2;
stateFlagTimeout(setState, 'foo', false, delay); stateFlagTimeout(setState, 'foo', false, delay);
expect(setState.callCount).toEqual(expectedSetStateCalls); expect(setState).toHaveBeenCalledTimes(2);
expect(setState.getCall(0).args).toEqual([{ foo: false }]); expect(setState).toHaveBeenNthCalledWith(1, { foo: false });
expect(setState.getCall(1).args).toEqual([{ foo: true }]); expect(setState).toHaveBeenNthCalledWith(2, { foo: true });
expect(setTimeout.callCount).toEqual(1); expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout.getCall(0).args[1]).toEqual(delay); expect(setTimeout).toHaveBeenCalledWith(expect.anything(), delay);
}); });
}); });

View file

@ -2,7 +2,6 @@ import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import { Card } from 'reactstrap'; import { Card } from 'reactstrap';
import * as sinon from 'sinon';
import createShortUrlVisits from '../../src/visits/ShortUrlVisits'; import createShortUrlVisits from '../../src/visits/ShortUrlVisits';
import MutedMessage from '../../src/utils/MuttedMessage'; import MutedMessage from '../../src/utils/MuttedMessage';
import GraphCard from '../../src/visits/GraphCard'; import GraphCard from '../../src/visits/GraphCard';
@ -14,7 +13,7 @@ describe('<ShortUrlVisits />', () => {
const processStatsFromVisits = () => ( const processStatsFromVisits = () => (
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} } { os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
); );
const getShortUrlVisitsMock = sinon.spy(); const getShortUrlVisitsMock = jest.fn();
const match = { const match = {
params: { shortCode: 'abc123' }, params: { shortCode: 'abc123' },
}; };
@ -37,11 +36,8 @@ describe('<ShortUrlVisits />', () => {
}; };
afterEach(() => { afterEach(() => {
getShortUrlVisitsMock.resetHistory(); getShortUrlVisitsMock.mockReset();
wrapper && wrapper.unmount();
if (wrapper) {
wrapper.unmount();
}
}); });
it('renders a preloader when visits are loading', () => { it('renders a preloader when visits are loading', () => {
@ -80,21 +76,19 @@ describe('<ShortUrlVisits />', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] }); const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
const graphs = wrapper.find(GraphCard); const graphs = wrapper.find(GraphCard);
const sortableBarGraphs = wrapper.find(SortableBarGraph); const sortableBarGraphs = wrapper.find(SortableBarGraph);
const expectedGraphsCount = 5;
expect(graphs.length + sortableBarGraphs.length).toEqual(expectedGraphsCount); expect(graphs.length + sortableBarGraphs.length).toEqual(5);
}); });
it('reloads visits when selected dates change', () => { it('reloads visits when selected dates change', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] }); const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
const dateInput = wrapper.find(DateInput).first(); const dateInput = wrapper.find(DateInput).first();
const expectedGetShortUrlVisitsCalls = 4;
dateInput.simulate('change', '2016-01-01T00:00:00+01:00'); dateInput.simulate('change', '2016-01-01T00:00:00+01:00');
dateInput.simulate('change', '2016-01-02T00:00:00+01:00'); dateInput.simulate('change', '2016-01-02T00:00:00+01:00');
dateInput.simulate('change', '2016-01-03T00:00:00+01:00'); dateInput.simulate('change', '2016-01-03T00:00:00+01:00');
expect(getShortUrlVisitsMock.callCount).toEqual(expectedGetShortUrlVisitsCalls); expect(getShortUrlVisitsMock).toHaveBeenCalledTimes(4);
expect(wrapper.state('startDate')).toEqual('2016-01-03T00:00:00+01:00'); expect(wrapper.state('startDate')).toEqual('2016-01-03T00:00:00+01:00');
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
getShortUrlDetail, getShortUrlDetail,
GET_SHORT_URL_DETAIL_START, GET_SHORT_URL_DETAIL_START,
@ -36,49 +35,34 @@ describe('shortUrlDetailReducer', () => {
describe('getShortUrlDetail', () => { describe('getShortUrlDetail', () => {
const buildApiClientMock = (returned) => ({ const buildApiClientMock = (returned) => ({
getShortUrl: sinon.fake.returns(returned), getShortUrl: jest.fn(() => returned),
}); });
const dispatchMock = sinon.spy(); const dispatchMock = jest.fn();
const getState = () => ({}); const getState = () => ({});
beforeEach(() => dispatchMock.resetHistory()); beforeEach(() => dispatchMock.mockReset());
it('dispatches start and error when promise is rejected', async () => { it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject()); const ShlinkApiClient = buildApiClientMock(Promise.reject());
const expectedDispatchCalls = 2;
await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState); await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args; expect(dispatchMock).toHaveBeenCalledTimes(2);
const { type: firstCallType } = firstCallArg; expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL_ERROR });
const [ secondCallArg ] = dispatchMock.getCall(1).args; expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
const { type: secondCallType } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrl.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_DETAIL_START);
expect(secondCallType).toEqual(GET_SHORT_URL_DETAIL_ERROR);
}); });
it('dispatches start and success when promise is resolved', async () => { it('dispatches start and success when promise is resolved', async () => {
const resolvedShortUrl = { longUrl: 'foo', shortCode: 'bar' }; const resolvedShortUrl = { longUrl: 'foo', shortCode: 'bar' };
const ShlinkApiClient = buildApiClientMock(Promise.resolve(resolvedShortUrl)); const ShlinkApiClient = buildApiClientMock(Promise.resolve(resolvedShortUrl));
const expectedDispatchCalls = 2;
await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState); await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args; expect(dispatchMock).toHaveBeenCalledTimes(2);
const { type: firstCallType } = firstCallArg; expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, shortUrl: resolvedShortUrl });
const [ secondCallArg ] = dispatchMock.getCall(1).args; expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
const { type: secondCallType, shortUrl } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrl.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_DETAIL_START);
expect(secondCallType).toEqual(GET_SHORT_URL_DETAIL);
expect(shortUrl).toEqual(resolvedShortUrl);
}); });
}); });
}); });

View file

@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, { import reducer, {
getShortUrlVisits, getShortUrlVisits,
cancelGetShortUrlVisits, cancelGetShortUrlVisits,
@ -53,57 +52,42 @@ describe('shortUrlVisitsReducer', () => {
describe('getShortUrlVisits', () => { describe('getShortUrlVisits', () => {
const buildApiClientMock = (returned) => ({ const buildApiClientMock = (returned) => ({
getShortUrlVisits: typeof returned === 'function' ? sinon.fake(returned) : sinon.fake.returns(returned), getShortUrlVisits: jest.fn(typeof returned === 'function' ? returned : () => returned),
}); });
const dispatchMock = sinon.spy(); const dispatchMock = jest.fn();
const getState = () => ({ const getState = () => ({
shortUrlVisits: { cancelVisits: false }, shortUrlVisits: { cancelVisits: false },
}); });
beforeEach(() => dispatchMock.resetHistory()); beforeEach(() => dispatchMock.mockReset());
it('dispatches start and error when promise is rejected', async () => { it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject()); const ShlinkApiClient = buildApiClientMock(Promise.reject());
const expectedDispatchCalls = 2;
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState); await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args; expect(dispatchMock).toHaveBeenCalledTimes(2);
const { type: firstCallType } = firstCallArg; expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_VISITS_ERROR });
const [ secondCallArg ] = dispatchMock.getCall(1).args; expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(1);
const { type: secondCallType } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrlVisits.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_VISITS_START);
expect(secondCallType).toEqual(GET_SHORT_URL_VISITS_ERROR);
}); });
it('dispatches start and success when promise is resolved', async () => { it('dispatches start and success when promise is resolved', async () => {
const resolvedVisits = [{}, {}]; const visits = [{}, {}];
const ShlinkApiClient = buildApiClientMock(Promise.resolve({ const ShlinkApiClient = buildApiClientMock(Promise.resolve({
data: resolvedVisits, data: visits,
pagination: { pagination: {
currentPage: 1, currentPage: 1,
pagesCount: 1, pagesCount: 1,
}, },
})); }));
const expectedDispatchCalls = 2;
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState); await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args; expect(dispatchMock).toHaveBeenCalledTimes(2);
const { type: firstCallType } = firstCallArg; expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_VISITS, visits });
const [ secondCallArg ] = dispatchMock.getCall(1).args; expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(1);
const { type: secondCallType, visits } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrlVisits.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_VISITS_START);
expect(secondCallType).toEqual(GET_SHORT_URL_VISITS);
expect(visits).toEqual(resolvedVisits);
}); });
it('performs multiple API requests when response contains more pages', async () => { it('performs multiple API requests when response contains more pages', async () => {
@ -119,11 +103,10 @@ describe('shortUrlVisitsReducer', () => {
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState); await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ secondCallArg ] = dispatchMock.getCall(1).args; expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(expectedRequests);
const { visits } = secondCallArg; expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
visits: [{}, {}, {}, {}, {}, {}],
expect(ShlinkApiClient.getShortUrlVisits.callCount).toEqual(expectedRequests); }));
expect(visits).toEqual([{}, {}, {}, {}, {}, {}]);
}); });
}); });