From e1333f5e001edc7eaa64fff2c8eb33c9b1698a2c Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Thu, 18 Apr 2019 10:52:35 -0400 Subject: [PATCH 1/9] move value by prop in providers to consistent interface --- package-lock.json | 79 ++++++++++++++++++++++++------------- src/contexts/Config.jsx | 11 +++--- src/contexts/Network.jsx | 11 +++--- src/contexts/RootDialog.jsx | 2 +- 4 files changed, 62 insertions(+), 41 deletions(-) diff --git a/package-lock.json b/package-lock.json index d48da6c8e0..ad1c8266d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2143,6 +2143,7 @@ "version": "2.13.2", "resolved": "https://registry.npmjs.org/airbnb-prop-types/-/airbnb-prop-types-2.13.2.tgz", "integrity": "sha512-2FN6DlHr6JCSxPPi25EnqGaXC4OC3/B3k1lCd6MMYrZ51/Gf/1qDfaR+JElzWa+Tl7cY2aYOlsYJGFeQyVHIeQ==", + "dev": true, "requires": { "array.prototype.find": "^2.0.4", "function.prototype.name": "^1.1.0", @@ -2160,6 +2161,7 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/object.entries/-/object.entries-1.1.0.tgz", "integrity": "sha512-l+H6EQ8qzGRxbkHOd5I/aHRhHDKoQXQ8g0BYt4uSweQU1/J6dZUOyWh9a2Vky35YCKjzmgxOzta2hH6kf9HuXA==", + "dev": true, "requires": { "define-properties": "^1.1.3", "es-abstract": "^1.12.0", @@ -2171,6 +2173,7 @@ "version": "15.7.2", "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "dev": true, "requires": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", @@ -2180,7 +2183,8 @@ "react-is": { "version": "16.8.6", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.6.tgz", - "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==" + "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==", + "dev": true } } }, @@ -2215,7 +2219,7 @@ }, "ansi-colors": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/ansi-colors/-/ansi-colors-1.1.0.tgz", "integrity": "sha512-SFKX67auSNoVR38N3L+nvsPjOE0bybKTYbkf5tRvushrAPQ9V75huw0ZxBkKVeRU9kqH3d6HA4xTckbwZ4ixmA==", "requires": { "ansi-wrap": "^0.1.0" @@ -2736,6 +2740,7 @@ "version": "2.0.4", "resolved": "https://registry.npmjs.org/array.prototype.find/-/array.prototype.find-2.0.4.tgz", "integrity": "sha1-VWpcU2LAhkgyPdrrnenRS8GGTJA=", + "dev": true, "requires": { "define-properties": "^1.1.2", "es-abstract": "^1.7.0" @@ -3234,12 +3239,12 @@ }, "babel-plugin-syntax-class-properties": { "version": "6.13.0", - "resolved": "https://registry.npmjs.org/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz", + "resolved": "http://registry.npmjs.org/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz", "integrity": "sha1-1+sjt5oxf4VDlixQW4J8fWysJ94=" }, "babel-plugin-syntax-flow": { "version": "6.18.0", - "resolved": "https://registry.npmjs.org/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz", + "resolved": "http://registry.npmjs.org/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz", "integrity": "sha1-TDqyCiryaqIM0lmVw5jE63AxDI0=" }, "babel-plugin-syntax-jsx": { @@ -5155,6 +5160,7 @@ "version": "1.1.3", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "dev": true, "requires": { "object-keys": "^1.0.12" }, @@ -5162,7 +5168,8 @@ "object-keys": { "version": "1.0.12", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", - "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==" + "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", + "dev": true } } }, @@ -5414,7 +5421,7 @@ }, "readable-stream": { "version": "2.3.6", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "requires": { "core-util-is": "~1.0.0", @@ -5618,6 +5625,7 @@ "version": "1.12.1", "resolved": "https://registry.npmjs.org/enzyme-adapter-react-16/-/enzyme-adapter-react-16-1.12.1.tgz", "integrity": "sha512-GB61gvY97XvrA6qljExGY+lgI6BBwz+ASLaRKct9VQ3ozu0EraqcNn3CcrUckSGIqFGa1+CxO5gj5is5t3lwrw==", + "dev": true, "requires": { "enzyme-adapter-utils": "^1.11.0", "object.assign": "^4.1.0", @@ -5632,6 +5640,7 @@ "version": "15.7.2", "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "dev": true, "requires": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", @@ -5641,7 +5650,8 @@ "react-is": { "version": "16.8.6", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.6.tgz", - "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==" + "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==", + "dev": true } } }, @@ -5649,6 +5659,7 @@ "version": "1.11.0", "resolved": "https://registry.npmjs.org/enzyme-adapter-utils/-/enzyme-adapter-utils-1.11.0.tgz", "integrity": "sha512-0VZeoE9MNx+QjTfsjmO1Mo+lMfunucYB4wt5ficU85WB/LoetTJrbuujmHP3PJx6pSoaAuLA+Mq877x4LoxdNg==", + "dev": true, "requires": { "airbnb-prop-types": "^2.12.0", "function.prototype.name": "^1.1.0", @@ -5662,6 +5673,7 @@ "version": "15.7.2", "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "dev": true, "requires": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", @@ -5671,7 +5683,8 @@ "react-is": { "version": "16.8.6", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.6.tgz", - "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==" + "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==", + "dev": true } } }, @@ -5705,6 +5718,7 @@ "version": "1.12.0", "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.12.0.tgz", "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", + "dev": true, "requires": { "es-to-primitive": "^1.1.1", "function-bind": "^1.1.1", @@ -5717,6 +5731,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", + "dev": true, "requires": { "is-callable": "^1.1.4", "is-date-object": "^1.0.1", @@ -6625,7 +6640,7 @@ }, "readable-stream": { "version": "2.3.6", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "requires": { "core-util-is": "~1.0.0", @@ -7536,12 +7551,14 @@ "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true }, "function.prototype.name": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/function.prototype.name/-/function.prototype.name-1.1.0.tgz", "integrity": "sha512-Bs0VRrTz4ghD8pTmbJQD1mZ8A/mN0ur/jGz+A6FBxPDUPkm1tNfF6bhTYPA7i7aF4lZJVr+OXTNNrnnIl58Wfg==", + "dev": true, "requires": { "define-properties": "^1.1.2", "function-bind": "^1.1.1", @@ -7751,6 +7768,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, "requires": { "function-bind": "^1.1.1" } @@ -7772,7 +7790,8 @@ "has-symbols": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", - "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=" + "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", + "dev": true }, "has-unicode": { "version": "2.0.1", @@ -8734,7 +8753,8 @@ "is-callable": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", - "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==" + "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", + "dev": true }, "is-ci": { "version": "2.0.0", @@ -8768,7 +8788,8 @@ "is-date-object": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", - "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=" + "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", + "dev": true }, "is-decimal": { "version": "1.0.2", @@ -8903,6 +8924,7 @@ "version": "1.0.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", + "dev": true, "requires": { "has": "^1.0.1" } @@ -8934,6 +8956,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", + "dev": true, "requires": { "has-symbols": "^1.0.0" } @@ -10583,7 +10606,7 @@ }, "kind-of": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/kind-of/-/kind-of-1.1.0.tgz", "integrity": "sha1-FAo9LUGjbS78+pN3tiwk+ElaXEQ=" }, "kleur": { @@ -11773,7 +11796,8 @@ "object-is": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.0.1.tgz", - "integrity": "sha1-CqYOyZiaCz7Xlc9NBvYs8a1lObY=" + "integrity": "sha1-CqYOyZiaCz7Xlc9NBvYs8a1lObY=", + "dev": true }, "object-keys": { "version": "0.4.0", @@ -11801,6 +11825,7 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz", "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==", + "dev": true, "requires": { "define-properties": "^1.1.2", "function-bind": "^1.1.1", @@ -11811,7 +11836,8 @@ "object-keys": { "version": "1.0.12", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", - "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==" + "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", + "dev": true } } }, @@ -11831,6 +11857,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/object.fromentries/-/object.fromentries-2.0.0.tgz", "integrity": "sha512-9iLiI6H083uiqUuvzyY6qrlmc/Gz8hLQFOcb/Ri/0xXFkSNS3ctV+CbE6yM2+AnkYfOB3dGjdzC0wrMLIhQICA==", + "dev": true, "requires": { "define-properties": "^1.1.2", "es-abstract": "^1.11.0", @@ -11869,6 +11896,7 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/object.values/-/object.values-1.1.0.tgz", "integrity": "sha512-8mf0nKLAoFX6VlNVdhGj31SVYpaNFtUnuoOXWyFEstsWRgU837AK+JYM0iAxwkSzGRbwn8cbFmgbyxj1j4VbXg==", + "dev": true, "requires": { "define-properties": "^1.1.3", "es-abstract": "^1.12.0", @@ -12699,6 +12727,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/prop-types-exact/-/prop-types-exact-1.2.0.tgz", "integrity": "sha512-K+Tk3Kd9V0odiXFP9fwDHUYRyvK3Nun3GVyPapSIs5OBkITAm15W0CPFD/YKTkMUAbc0b9CUwRQp2ybiBIq+eA==", + "dev": true, "requires": { "has": "^1.0.3", "object.assign": "^4.1.0", @@ -12987,6 +13016,7 @@ "version": "16.8.6", "resolved": "https://registry.npmjs.org/react-test-renderer/-/react-test-renderer-16.8.6.tgz", "integrity": "sha512-H2srzU5IWYT6cZXof6AhUcx/wEyJddQ8l7cLM/F7gDXYyPr4oq+vCIxJYXVGhId1J706sqziAjuOEjyNkfgoEw==", + "dev": true, "requires": { "object-assign": "^4.1.1", "prop-types": "^15.6.2", @@ -12997,12 +13027,14 @@ "react-is": { "version": "16.8.6", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.6.tgz", - "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==" + "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==", + "dev": true }, "scheduler": { "version": "0.13.6", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.13.6.tgz", "integrity": "sha512-IWnObHt413ucAYKsD9J1QShUKkbKLQQHdxRyw73sw4FN26iWr3DY/H34xGPe4nmL1DwXyWmSWmMrA9TfQbE/XQ==", + "dev": true, "requires": { "loose-envify": "^1.1.0", "object-assign": "^4.1.1" @@ -13423,7 +13455,8 @@ "reflect.ownkeys": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/reflect.ownkeys/-/reflect.ownkeys-0.2.0.tgz", - "integrity": "sha1-dJrO7H8/34tj+SegSAnpDFwLNGA=" + "integrity": "sha1-dJrO7H8/34tj+SegSAnpDFwLNGA=", + "dev": true }, "regenerate": { "version": "1.4.0", @@ -13934,16 +13967,6 @@ "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" }, - "scheduler": { - "version": "0.10.0", - "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.10.0.tgz", - "integrity": "sha512-+TSTVTCBAA3h8Anei3haDc1IRwMeDmtI/y/o3iBe3Mjl2vwYF9DtPDt929HyRmV/e7au7CLu8sc4C4W0VOs29w==", - "dev": true, - "requires": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" - } - }, "schema-utils": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-1.0.0.tgz", diff --git a/src/contexts/Config.jsx b/src/contexts/Config.jsx index 3c71ccf21d..63bc2525a8 100644 --- a/src/contexts/Config.jsx +++ b/src/contexts/Config.jsx @@ -15,7 +15,8 @@ class Provider extends Component { custom_virtualenvs: null, version: null, custom_logo: null, - custom_login_info: null + custom_login_info: null, + ...props.value } }; @@ -66,14 +67,12 @@ class Provider extends Component { } render () { - const { - value: stateValue - } = this.state; + const { value } = this.state; - const { value: propsValue, children } = this.props; + const { children } = this.props; return ( - + {children} ); diff --git a/src/contexts/Network.jsx b/src/contexts/Network.jsx index 2fba529e3e..5407346c45 100644 --- a/src/contexts/Network.jsx +++ b/src/contexts/Network.jsx @@ -26,7 +26,8 @@ class Provider extends Component { this.handle404(); } return (err.response.status === 401 || err.response.status === 404); - } + }, + ...props.value } }; } @@ -58,14 +59,12 @@ class Provider extends Component { } render () { - const { - value: stateValue - } = this.state; + const { value } = this.state; - const { value: propsValue, children } = this.props; + const { children } = this.props; return ( - + {children} ); diff --git a/src/contexts/RootDialog.jsx b/src/contexts/RootDialog.jsx index 6a938b1e99..d393da068d 100644 --- a/src/contexts/RootDialog.jsx +++ b/src/contexts/RootDialog.jsx @@ -17,7 +17,7 @@ export class RootDialogProvider extends Component { const { value } = this.state; this.setState({ value: { ...value, title: null, bodyText: null, variant: null } }); }, - ...props.value, + ...props.value } }; } From 5030eb35b6944c4398a8ae33c894721c242788b4 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Thu, 18 Apr 2019 13:10:17 -0400 Subject: [PATCH 2/9] migrate App.jsx to mountwithContext --- __tests__/App.test.jsx | 105 +++++++++++------------------------- __tests__/enzymeHelpers.jsx | 8 ++- 2 files changed, 38 insertions(+), 75 deletions(-) diff --git a/__tests__/App.test.jsx b/__tests__/App.test.jsx index 9712833be4..29f0f21fa0 100644 --- a/__tests__/App.test.jsx +++ b/__tests__/App.test.jsx @@ -1,50 +1,36 @@ import React from 'react'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; -import { mount } from 'enzyme'; +import { mountWithContexts } from './enzymeHelpers'; + import { asyncFlush } from '../jest.setup'; -import { ConfigProvider } from '../src/contexts/Config'; -import { NetworkProvider } from '../src/contexts/Network'; - -import App, { _App } from '../src/App'; - -const networkProviderValue = { api: {}, handleHttpError: () => {} }; +import App from '../src/App'; describe('', () => { test('expected content is rendered', () => { - const appWrapper = mount( - - - - - ( - routeGroups.map(({ groupId }) => (
)) - )} - /> - - - - + const appWrapper = mountWithContexts( + ( + routeGroups.map(({ groupId }) => (
)) + )} + /> ); // page components @@ -69,17 +55,7 @@ describe('', () => { const config = { ansible_version, version }; - const wrapper = mount( - - - - - - - - - - ); + const wrapper = mountWithContexts(, { context: { config } }); // open about modal const aboutDropdown = 'Dropdown QuestionCircleIcon'; @@ -106,19 +82,8 @@ describe('', () => { }); test('onNavToggle sets state.isNavOpen to opposite', () => { - const appWrapper = mount( - - - - - - - - - - ).find('App'); + const appWrapper = mountWithContexts().find('App'); const { onNavToggle } = appWrapper.instance(); - [true, false, true, false, true].forEach(expected => { expect(appWrapper.state().isNavOpen).toBe(expected); onNavToggle(); @@ -128,17 +93,9 @@ describe('', () => { test('onLogout makes expected call to api client', async (done) => { const logout = jest.fn(() => Promise.resolve()); - const appWrapper = mount( - - - - - <_App api={{ logout }} handleHttpError={() => {}} /> - - - - - ).find('App'); + const appWrapper = mountWithContexts(, { + context: { network: { api: { logout }, handleHttpError: () => {} } } + }).find('App'); appWrapper.instance().onLogout(); await asyncFlush(); diff --git a/__tests__/enzymeHelpers.jsx b/__tests__/enzymeHelpers.jsx index c4e31909a7..b49dcb17f7 100644 --- a/__tests__/enzymeHelpers.jsx +++ b/__tests__/enzymeHelpers.jsx @@ -45,6 +45,12 @@ const defaultContexts = { push: () => {}, replace: () => {}, createHref: () => {}, + location: { + hash: '', + pathname: '', + search: '', + state: '', + } }, route: { location: { @@ -58,7 +64,7 @@ const defaultContexts = { isExact: false, path: '', url: '', - }, + } }, toJSON: () => '/router/', }, From ce49cb9ba4c55533e0c8d6dd2efae50631a067f6 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Thu, 18 Apr 2019 13:53:05 -0400 Subject: [PATCH 3/9] fix config context if value passed by props not getting overwritten --- src/contexts/Config.jsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/contexts/Config.jsx b/src/contexts/Config.jsx index 63bc2525a8..9fe44da64c 100644 --- a/src/contexts/Config.jsx +++ b/src/contexts/Config.jsx @@ -24,7 +24,10 @@ class Provider extends Component { } componentDidMount () { - this.fetchConfig(); + const { value } = this.props; + if (!value) { + this.fetchConfig(); + } } async fetchConfig () { From a6f79c646dd4066986a3e4e193d06593dfecd314 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Thu, 18 Apr 2019 13:54:41 -0400 Subject: [PATCH 4/9] update organization add test to use mountWithContexts --- .../screens/OrganizationAdd.test.jsx | 116 ++++-------------- 1 file changed, 23 insertions(+), 93 deletions(-) diff --git a/__tests__/pages/Organizations/screens/OrganizationAdd.test.jsx b/__tests__/pages/Organizations/screens/OrganizationAdd.test.jsx index 5f7765966e..6a27b7ff1e 100644 --- a/__tests__/pages/Organizations/screens/OrganizationAdd.test.jsx +++ b/__tests__/pages/Organizations/screens/OrganizationAdd.test.jsx @@ -1,12 +1,8 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; -import { ConfigProvider } from '../../../../src/contexts/Config'; -import { NetworkProvider } from '../../../../src/contexts/Network'; +import { mountWithContexts } from '../../../enzymeHelpers'; -import { _OrganizationAdd } from '../../../../src/pages/Organizations/screens/OrganizationAdd'; +import OrganizationAdd from '../../../../src/pages/Organizations/screens/OrganizationAdd'; const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); @@ -29,25 +25,15 @@ describe('', () => { }); test('handleSubmit should post to api', () => { - const wrapper = mount( - - - - - <_OrganizationAdd api={api} /> - - - - - ); - + const wrapper = mountWithContexts(, { + context: { network: networkProviderValue } + }); const updatedOrgData = { name: 'new name', description: 'new description', custom_virtualenv: 'Buzz', }; wrapper.find('OrganizationForm').prop('handleSubmit')(updatedOrgData, [], []); - expect(api.createOrganization).toHaveBeenCalledWith(updatedOrgData); }); @@ -55,21 +41,11 @@ describe('', () => { const history = { push: jest.fn(), }; - const wrapper = mount( - - - - - <_OrganizationAdd api={api} history={history} /> - - - - - ); - + const wrapper = mountWithContexts(, { + context: { router: { history } } + }); expect(history.push).not.toHaveBeenCalled(); wrapper.find('button[aria-label="Cancel"]').prop('onClick')(); - expect(history.push).toHaveBeenCalledWith('/organizations'); }); @@ -77,21 +53,11 @@ describe('', () => { const history = { push: jest.fn(), }; - const wrapper = mount( - - - - - <_OrganizationAdd api={api} history={history} /> - - - - - ); - + const wrapper = mountWithContexts(, { + context: { router: { history } } + }); expect(history.push).not.toHaveBeenCalled(); wrapper.find('button[aria-label="Close"]').prop('onClick')(); - expect(history.push).toHaveBeenCalledWith('/organizations'); }); @@ -113,37 +79,18 @@ describe('', () => { ...orgData, } }); - const wrapper = mount( - - - - - <_OrganizationAdd api={api} history={history} /> - - - - - ); - + const wrapper = mountWithContexts(, { + context: { router: { history }, network: networkProviderValue } + }); wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [], []); await sleep(0); - expect(history.push).toHaveBeenCalledWith('/organizations/5'); }); test('handleSubmit should post instance groups', async () => { - const wrapper = mount( - - - - - <_OrganizationAdd api={api} /> - - - - - ); - + const wrapper = mountWithContexts(, { + context: { network: networkProviderValue } + }); const orgData = { name: 'new name', description: 'new description', @@ -160,7 +107,6 @@ describe('', () => { }); wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [3], []); await sleep(0); - expect(api.associateInstanceGroup) .toHaveBeenCalledWith('/api/v2/organizations/5/instance_groups', 3); }); @@ -169,17 +115,9 @@ describe('', () => { const config = { custom_virtualenvs: ['foo', 'bar'], }; - const wrapper = mount( - - - - - <_OrganizationAdd api={api} /> - - - - - ).find('OrganizationAdd').find('AnsibleSelect'); + const wrapper = mountWithContexts(, { + context: { network: networkProviderValue, config } + }).find('AnsibleSelect'); expect(wrapper.find('FormSelect')).toHaveLength(1); expect(wrapper.find('FormSelectOption')).toHaveLength(2); }); @@ -188,17 +126,9 @@ describe('', () => { const config = { custom_virtualenvs: [], }; - const wrapper = mount( - - - - - <_OrganizationAdd api={api} /> - - - - - ).find('OrganizationAdd').find('AnsibleSelect'); + const wrapper = mountWithContexts(, { + context: { network: networkProviderValue, config } + }).find('AnsibleSelect'); expect(wrapper.find('FormSelect')).toHaveLength(0); }); }); From 54499dbf690db958c1290f615236dd4d3b5768ae Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Thu, 18 Apr 2019 17:18:19 -0400 Subject: [PATCH 5/9] update OrganizsationAccess and OrganizationAccessList w mountWithContexts --- .../OrganizationAccessList.test.jsx | 102 ++++++------------ .../Organization/Organization.test.jsx | 15 +-- .../Organization/OrganizationAccess.test.jsx | 55 +++------- .../components/OrganizationAccessList.jsx | 9 +- .../screens/Organization/Organization.jsx | 6 +- .../Organization/OrganizationAccess.jsx | 9 -- 6 files changed, 54 insertions(+), 142 deletions(-) diff --git a/__tests__/pages/Organizations/components/OrganizationAccessList.test.jsx b/__tests__/pages/Organizations/components/OrganizationAccessList.test.jsx index 54288159d6..42836b436e 100644 --- a/__tests__/pages/Organizations/components/OrganizationAccessList.test.jsx +++ b/__tests__/pages/Organizations/components/OrganizationAccessList.test.jsx @@ -1,8 +1,6 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../../../enzymeHelpers'; import OrganizationAccessList, { _OrganizationAccessList } from '../../../../src/pages/Organizations/components/OrganizationAccessList'; const mockData = [ @@ -31,32 +29,20 @@ describe('', () => { }); test('initially renders succesfully', () => { - mount( - - - {}} - removeRole={() => {}} - /> - - + mountWithContexts( + {}} + removeRole={() => {}} + /> ); }); test('api response data passed to component gets set to state properly', (done) => { - const wrapper = mount( - - - ({ data: { count: 1, results: mockData } })} - removeRole={() => {}} - /> - - + const wrapper = mountWithContexts( + ({ data: { count: 1, results: mockData } })} + removeRole={() => {}} + /> ).find('OrganizationAccessList'); setImmediate(() => { @@ -67,18 +53,11 @@ describe('', () => { test('onSort being passed properly to DataListToolbar component', async (done) => { const onSort = jest.spyOn(_OrganizationAccessList.prototype, 'onSort'); - const wrapper = mount( - - - <_OrganizationAccessList - match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }} - location={{ search: '', pathname: '/organizations/1/access' }} - getAccessList={() => ({ data: { count: 1, results: mockData } })} - removeRole={() => {}} - handleHttpError={() => {}} - /> - - + const wrapper = mountWithContexts( + ({ data: { count: 1, results: mockData } })} + removeRole={() => {}} + /> ).find('OrganizationAccessList'); expect(onSort).not.toHaveBeenCalled(); @@ -91,17 +70,11 @@ describe('', () => { }); test('getTeamRoles returns empty array if dataset is missing team_id attribute', (done) => { - const wrapper = mount( - - - ({ data: { count: 1, results: mockData } })} - removeRole={() => {}} - /> - - + const wrapper = mountWithContexts( + ({ data: { count: 1, results: mockData } })} + removeRole={() => {}} + /> ).find('OrganizationAccessList'); setImmediate(() => { @@ -117,18 +90,11 @@ describe('', () => { const handleWarning = jest.spyOn(_OrganizationAccessList.prototype, 'handleWarning'); const confirmDelete = jest.spyOn(_OrganizationAccessList.prototype, 'confirmDelete'); const removeRole = jest.spyOn(_OrganizationAccessList.prototype, 'removeAccessRole'); - const wrapper = mount( - - - <_OrganizationAccessList - match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }} - location={{ search: '', pathname: '/organizations/1/access' }} - getAccessList={() => ({ data: { count: 1, results: mockData } })} - removeRole={() => {}} - handleHttpError={() => {}} - /> - - + const wrapper = mountWithContexts( + ({ data: { count: 1, results: mockData } })} + removeRole={() => {}} + /> ).find('OrganizationAccessList'); expect(handleWarning).not.toHaveBeenCalled(); expect(confirmDelete).not.toHaveBeenCalled(); @@ -147,17 +113,11 @@ describe('', () => { }); test('state is set appropriately when a user tries deleting a role', (done) => { - const wrapper = mount( - - - <_OrganizationAccessList - match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }} - location={{ search: '', pathname: '/organizations/1/access' }} - getAccessList={() => ({ data: { count: 1, results: mockData } })} - removeRole={() => {}} - /> - - + const wrapper = mountWithContexts( + ({ data: { count: 1, results: mockData } })} + removeRole={() => {}} + /> ).find('OrganizationAccessList'); setImmediate(() => { diff --git a/__tests__/pages/Organizations/screens/Organization/Organization.test.jsx b/__tests__/pages/Organizations/screens/Organization/Organization.test.jsx index ac147e2631..25a9da73fd 100644 --- a/__tests__/pages/Organizations/screens/Organization/Organization.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/Organization.test.jsx @@ -1,20 +1,9 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../../../../enzymeHelpers'; import Organization from '../../../../../src/pages/Organizations/screens/Organization/Organization'; describe('', () => { test('initially renders succesfully', () => { - mount( - - - - - - ); + mountWithContexts(); }); }); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationAccess.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationAccess.test.jsx index 837eb3814e..fd8748d9e8 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationAccess.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationAccess.test.jsx @@ -1,51 +1,26 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; - +import { mountWithContexts } from '../../../../enzymeHelpers'; import OrganizationAccess from '../../../../../src/pages/Organizations/screens/Organization/OrganizationAccess'; -const mockAPIAccessList = { - foo: 'bar', -}; - -const mockGetOrganizationAccessList = () => Promise.resolve(mockAPIAccessList); - -const mockResponse = { - status: 'success', -}; - -const mockRemoveRole = () => Promise.resolve(mockResponse); - describe('', () => { test('initially renders succesfully', () => { - mount( - - - - ); + mountWithContexts(); }); test('passed methods as props are called appropriately', async () => { - const wrapper = mount( - - - - ).find('OrganizationAccess'); + const mockAPIAccessList = { + foo: 'bar', + }; + const mockResponse = { + status: 'success', + }; + const wrapper = mountWithContexts(, { context: { network: { + api: { + getOrganizationAccessList: () => Promise.resolve(mockAPIAccessList), + disassociate: () => Promise.resolve(mockResponse) + }, + handleHttpError: () => {} + } } }).find('OrganizationAccess'); const accessList = await wrapper.instance().getOrgAccessList(); expect(accessList).toEqual(mockAPIAccessList); const resp = await wrapper.instance().removeRole(2, 3, 'users'); diff --git a/src/pages/Organizations/components/OrganizationAccessList.jsx b/src/pages/Organizations/components/OrganizationAccessList.jsx index cc53fa50f1..5aecddd279 100644 --- a/src/pages/Organizations/components/OrganizationAccessList.jsx +++ b/src/pages/Organizations/components/OrganizationAccessList.jsx @@ -10,7 +10,8 @@ import { I18n, i18nMark } from '@lingui/react'; import { t, Trans } from '@lingui/macro'; import { - Link + Link, + withRouter } from 'react-router-dom'; import { withNetwork } from '../../../contexts/Network'; @@ -160,8 +161,8 @@ class OrganizationAccessList extends React.Component { } getQueryParams (overrides = {}) { - const { location } = this.props; - const { search } = location; + const { history } = this.props; + const { search } = history.location; const searchParams = parseQueryString(search.substring(1)); @@ -422,4 +423,4 @@ OrganizationAccessList.propTypes = { }; export { OrganizationAccessList as _OrganizationAccessList }; -export default withNetwork(OrganizationAccessList); +export default withRouter(withNetwork(OrganizationAccessList)); diff --git a/src/pages/Organizations/screens/Organization/Organization.jsx b/src/pages/Organizations/screens/Organization/Organization.jsx index 2ba53ece98..2ff186e3db 100644 --- a/src/pages/Organizations/screens/Organization/Organization.jsx +++ b/src/pages/Organizations/screens/Organization/Organization.jsx @@ -148,11 +148,7 @@ class Organization extends Component { ( - + )} /> ); } From 667cbb0c203133486968e1280748b676e6d767e5 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 22 Apr 2019 15:11:28 -0400 Subject: [PATCH 6/9] update the rest of the organizations tests --- .../components/OrganizationListItem.test.jsx | 4 +- .../components/OrganizationTeamsList.test.jsx | 76 ++++++++----------- .../Organization/OrganizationDetail.test.jsx | 76 +++++-------------- .../Organization/OrganizationEdit.test.jsx | 75 +++++++----------- .../OrganizationNotifications.test.jsx | 75 ++++++++---------- .../Organization/OrganizationTeams.test.jsx | 68 ++++++----------- .../NotificationsList/Notifications.list.jsx | 3 +- .../Organization/OrganizationDetail.jsx | 4 +- .../OrganizationNotifications.jsx | 9 --- 9 files changed, 138 insertions(+), 252 deletions(-) diff --git a/__tests__/pages/Organizations/components/OrganizationListItem.test.jsx b/__tests__/pages/Organizations/components/OrganizationListItem.test.jsx index a1bea32b3f..210979646b 100644 --- a/__tests__/pages/Organizations/components/OrganizationListItem.test.jsx +++ b/__tests__/pages/Organizations/components/OrganizationListItem.test.jsx @@ -1,12 +1,12 @@ import React from 'react'; -import { mount } from 'enzyme'; import { MemoryRouter } from 'react-router-dom'; import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../../../enzymeHelpers'; import OrganizationListItem from '../../../../src/pages/Organizations/components/OrganizationListItem'; describe('', () => { test('initially renders succesfully', () => { - mount( + mountWithContexts( diff --git a/__tests__/pages/Organizations/components/OrganizationTeamsList.test.jsx b/__tests__/pages/Organizations/components/OrganizationTeamsList.test.jsx index 41282262f5..4f0924826e 100644 --- a/__tests__/pages/Organizations/components/OrganizationTeamsList.test.jsx +++ b/__tests__/pages/Organizations/components/OrganizationTeamsList.test.jsx @@ -1,8 +1,6 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter, Router } from 'react-router-dom'; import { createMemoryHistory } from 'history'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../../../enzymeHelpers'; import { sleep } from '../../../testUtils'; import OrganizationTeamsList from '../../../../src/pages/Organizations/components/OrganizationTeamsList'; @@ -20,20 +18,16 @@ describe('', () => { }); test('initially renders succesfully', () => { - mount( - - - - - + mountWithContexts( + ); }); @@ -42,20 +36,16 @@ describe('', () => { const history = createMemoryHistory({ initialEntries: ['/organizations/1/teams'], }); - const wrapper = mount( - - - - - + const wrapper = mountWithContexts( + , { context: { router: { history } } } ); const toolbar = wrapper.find('DataListToolbar'); @@ -78,20 +68,16 @@ describe('', () => { const history = createMemoryHistory({ initialEntries: ['/organizations/1/teams'], }); - const wrapper = mount( - - - - - + const wrapper = mountWithContexts( + , { context: { router: { history } } } ); const pagination = wrapper.find('Pagination'); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationDetail.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationDetail.test.jsx index 5e66115d03..177ff714b1 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationDetail.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationDetail.test.jsx @@ -1,7 +1,5 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../../../../enzymeHelpers'; import OrganizationDetail from '../../../../../src/pages/Organizations/screens/Organization/OrganizationDetail'; describe('', () => { @@ -14,33 +12,21 @@ describe('', () => { }; test('initially renders succesfully', () => { - mount( - - - - - + mountWithContexts( + ); }); test('should request instance groups from api', () => { const getOrganizationInstanceGroups = jest.fn(); - mount( - - - {}} - organization={mockDetails} - /> - - + mountWithContexts( + , { context: { + network: { api: { getOrganizationInstanceGroups }, handleHttpError: () => {} } + } } ).find('OrganizationDetail'); expect(getOrganizationInstanceGroups).toHaveBeenCalledTimes(1); @@ -54,23 +40,12 @@ describe('', () => { const getOrganizationInstanceGroups = jest.fn(() => ( Promise.resolve({ data: { results: mockInstanceGroups } }) )); - const wrapper = mount( - - - {}} - api={{ - getOrganizationInstanceGroups - }} - /> - - + const wrapper = mountWithContexts( + , { context: { + network: { api: { getOrganizationInstanceGroups }, handleHttpError: () => {} } + } } ).find('OrganizationDetail'); await getOrganizationInstanceGroups(); @@ -78,19 +53,10 @@ describe('', () => { }); test('should render Details', async () => { - const wrapper = mount( - - - - - + const wrapper = mountWithContexts( + ); const detailWrapper = wrapper.find('Detail'); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx index 7aee6e5975..edd8296156 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationEdit.test.jsx @@ -1,17 +1,12 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../../../../enzymeHelpers'; -import { NetworkProvider } from '../../../../../src/contexts/Network'; - -import { _OrganizationEdit } from '../../../../../src/pages/Organizations/screens/Organization/OrganizationEdit'; +import OrganizationEdit from '../../../../../src/pages/Organizations/screens/Organization/OrganizationEdit'; const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); describe('', () => { let api; - let networkProviderValue; const mockData = { name: 'Foo', @@ -30,26 +25,16 @@ describe('', () => { associateInstanceGroup: jest.fn(), disassociate: jest.fn(), }; - - networkProviderValue = { - api, - handleHttpError: () => {} - }; }); test('handleSubmit should call api update', () => { - const wrapper = mount( - - - - <_OrganizationEdit - organization={mockData} - api={api} - handleHttpError={() => {}} - /> - - - + const wrapper = mountWithContexts( + , { context: { network: { + api, + handleHttpError: () => {} + } } } ); const updatedOrgData = { @@ -66,18 +51,13 @@ describe('', () => { }); test('handleSubmit associates and disassociates instance groups', async () => { - const wrapper = mount( - - - - <_OrganizationEdit - organization={mockData} - api={api} - handleHttpError={() => {}} - /> - - - + const wrapper = mountWithContexts( + , { context: { network: { + api, + handleHttpError: () => {} + } } } ); const updatedOrgData = { @@ -106,19 +86,16 @@ describe('', () => { const history = { push: jest.fn(), }; - const wrapper = mount( - - - - <_OrganizationEdit - organization={mockData} - api={api} - handleHttpError={() => {}} - history={history} - /> - - - + const wrapper = mountWithContexts( + , { context: { + network: { + api: { api }, + handleHttpError: () => {} + }, + router: { history } + } } ); expect(history.push).not.toHaveBeenCalled(); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.test.jsx index 3061c7e9b8..560565c931 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.test.jsx @@ -1,60 +1,45 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { _OrganizationNotifications } from '../../../../../src/pages/Organizations/screens/Organization/OrganizationNotifications'; +import { mountWithContexts } from '../../../../enzymeHelpers'; + +import OrganizationNotifications from '../../../../../src/pages/Organizations/screens/Organization/OrganizationNotifications'; describe('', () => { + let api; + + beforeEach(() => { + api = { + getOrganizationNotifications: jest.fn(), + getOrganizationNotificationSuccess: jest.fn(), + getOrganizationNotificationError: jest.fn(), + createOrganizationNotificationSuccess: jest.fn(), + createOrganizationNotificationError: jest.fn() + }; + }); + test('initially renders succesfully', () => { - mount( - - <_OrganizationNotifications - match={{ path: '/organizations/:id/notifications', url: '/organizations/1/notifications' }} - location={{ search: '', pathname: '/organizations/1/notifications' }} - params={{}} - api={{ - getOrganizationNotifications: jest.fn(), - getOrganizationNotificationSuccess: jest.fn(), - getOrganizationNotificationError: jest.fn(), - createOrganizationNotificationSuccess: jest.fn(), - createOrganizationNotificationError: jest.fn() - }} - handleHttpError={() => {}} - /> - + mountWithContexts( + , { context: { network: { + api, + handleHttpError: () => {} + } } } ); }); test('handles api requests', () => { - const getOrganizationNotifications = jest.fn(); - const getOrganizationNotificationSuccess = jest.fn(); - const getOrganizationNotificationError = jest.fn(); - const createOrganizationNotificationSuccess = jest.fn(); - const createOrganizationNotificationError = jest.fn(); - const wrapper = mount( - - <_OrganizationNotifications - match={{ path: '/organizations/:id/notifications', url: '/organizations/1/notifications' }} - location={{ search: '', pathname: '/organizations/1/notifications' }} - params={{}} - api={{ - getOrganizationNotifications, - getOrganizationNotificationSuccess, - getOrganizationNotificationError, - createOrganizationNotificationSuccess, - createOrganizationNotificationError - }} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + , { context: { network: { + api, + handleHttpError: () => {} + } } } ).find('OrganizationNotifications'); wrapper.instance().readOrgNotifications(1, { foo: 'bar' }); - expect(getOrganizationNotifications).toHaveBeenCalledWith(1, { foo: 'bar' }); + expect(api.getOrganizationNotifications).toHaveBeenCalledWith(1, { foo: 'bar' }); wrapper.instance().readOrgNotificationSuccess(1, { foo: 'bar' }); - expect(getOrganizationNotificationSuccess).toHaveBeenCalledWith(1, { foo: 'bar' }); + expect(api.getOrganizationNotificationSuccess).toHaveBeenCalledWith(1, { foo: 'bar' }); wrapper.instance().readOrgNotificationError(1, { foo: 'bar' }); - expect(getOrganizationNotificationError).toHaveBeenCalledWith(1, { foo: 'bar' }); + expect(api.getOrganizationNotificationError).toHaveBeenCalledWith(1, { foo: 'bar' }); wrapper.instance().createOrgNotificationSuccess(1, { id: 2 }); - expect(createOrganizationNotificationSuccess).toHaveBeenCalledWith(1, { id: 2 }); + expect(api.createOrganizationNotificationSuccess).toHaveBeenCalledWith(1, { id: 2 }); wrapper.instance().createOrgNotificationError(1, { id: 2 }); - expect(createOrganizationNotificationError).toHaveBeenCalledWith(1, { id: 2 }); + expect(api.createOrganizationNotificationError).toHaveBeenCalledWith(1, { id: 2 }); }); }); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx index 44effdca37..61b50bae57 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx @@ -1,12 +1,10 @@ import React from 'react'; -import { mount, shallow } from 'enzyme'; -import { MemoryRouter, Router } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; +import { shallow } from 'enzyme'; import { createMemoryHistory } from 'history'; +import { mountWithContexts } from '../../../../enzymeHelpers'; import { sleep } from '../../../../testUtils'; import OrganizationTeams, { _OrganizationTeams } from '../../../../../src/pages/Organizations/screens/Organization/OrganizationTeams'; import OrganizationTeamsList from '../../../../../src/pages/Organizations/components/OrganizationTeamsList'; -import { NetworkProvider } from '../../../../../src/contexts/Network'; const listData = { data: { @@ -38,19 +36,13 @@ describe('', () => { test('should load teams on mount', () => { const readOrganizationTeamsList = jest.fn(() => Promise.resolve(listData)); - mount( - - - {} }} - > - - - - + mountWithContexts( + , { context: { + network: { api: { readOrganizationTeamsList }, handleHttpError: () => {} } } + } ).find('OrganizationTeams'); expect(readOrganizationTeamsList).toHaveBeenCalledWith(1, { page: 1, @@ -61,19 +53,13 @@ describe('', () => { test('should pass fetched teams to list component', async () => { const readOrganizationTeamsList = jest.fn(() => Promise.resolve(listData)); - const wrapper = mount( - - - {} }} - > - - - - + const wrapper = mountWithContexts( + , { context: { + network: { api: { readOrganizationTeamsList }, handleHttpError: () => {} } } + } ); await sleep(0); @@ -105,19 +91,14 @@ describe('', () => { const history = createMemoryHistory({ initialEntries: ['/organizations/1/teams'], }); - const wrapper = mount( - - - {} }} - > - - - - + const wrapper = mountWithContexts( + , { context: { + network: { api: { readOrganizationTeamsList }, handleHttpError: () => {} }, + router: { history } + } } ); await sleep(0); @@ -132,7 +113,6 @@ describe('', () => { readOrganizationTeamsList.mockReturnValueOnce(page2Data); history.push('/organizations/1/teams?page=2'); - wrapper.setProps({ history }); await sleep(0); wrapper.update(); diff --git a/src/components/NotificationsList/Notifications.list.jsx b/src/components/NotificationsList/Notifications.list.jsx index cd3ea675b7..d7595d913e 100644 --- a/src/components/NotificationsList/Notifications.list.jsx +++ b/src/components/NotificationsList/Notifications.list.jsx @@ -3,6 +3,7 @@ import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; +import { withRouter } from 'react-router-dom'; import { Title, EmptyState, EmptyStateIcon, EmptyStateBody } from '@patternfly/react-core'; import { CubesIcon } from '@patternfly/react-icons'; import { I18n, i18nMark } from '@lingui/react'; @@ -344,4 +345,4 @@ Notifications.propTypes = { }; export { Notifications as _Notifications }; -export default withNetwork(Notifications); +export default withRouter(withNetwork(Notifications)); diff --git a/src/pages/Organizations/screens/Organization/OrganizationDetail.jsx b/src/pages/Organizations/screens/Organization/OrganizationDetail.jsx index 2771cf8c8e..e069a5e01b 100644 --- a/src/pages/Organizations/screens/Organization/OrganizationDetail.jsx +++ b/src/pages/Organizations/screens/Organization/OrganizationDetail.jsx @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { Link } from 'react-router-dom'; +import { Link, withRouter } from 'react-router-dom'; import { I18n } from '@lingui/react'; import { Trans, t } from '@lingui/macro'; @@ -178,4 +178,4 @@ class OrganizationDetail extends Component { } } -export default withNetwork(OrganizationDetail); +export default withRouter(withNetwork(OrganizationDetail)); diff --git a/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx b/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx index da658632c8..4f6cf13d6d 100644 --- a/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx +++ b/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx @@ -41,12 +41,6 @@ class OrganizationNotifications extends Component { } render () { - const { - location, - match, - history - } = this.props; - return ( ); } From 6f789b661fd8c82b68cdf279cab1d06681c6911c Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 22 Apr 2019 15:43:47 -0400 Subject: [PATCH 7/9] Fix NotificationList tests --- .../components/NotificationList.test.jsx | 184 +++++++----------- 1 file changed, 74 insertions(+), 110 deletions(-) diff --git a/__tests__/components/NotificationList.test.jsx b/__tests__/components/NotificationList.test.jsx index 1990dfab8e..47ae953746 100644 --- a/__tests__/components/NotificationList.test.jsx +++ b/__tests__/components/NotificationList.test.jsx @@ -1,139 +1,107 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { MemoryRouter } from 'react-router-dom'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../enzymeHelpers'; import Notifications, { _Notifications } from '../../src/components/NotificationsList/Notifications.list'; describe('', () => { test('initially renders succesfully', () => { - mount( - - - {}} - /> - - + mountWithContexts( + {}} + onReadNotifications={() => {}} + onReadSuccess={() => {}} + onCreateError={() => {}} + onCreateSuccess={() => {}} + /> ); }); test('fetches notifications on mount', () => { const spy = jest.spyOn(_Notifications.prototype, 'readNotifications'); - mount( - - <_Notifications - match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }} - location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }} - onReadError={jest.fn()} - onReadNotifications={jest.fn()} - onReadSuccess={jest.fn()} - onCreateError={jest.fn()} - onCreateSuccess={jest.fn()} - handleHttpError={() => {}} - /> - + mountWithContexts( + {}} + onReadNotifications={() => {}} + onReadSuccess={() => {}} + onCreateError={() => {}} + onCreateSuccess={() => {}} + /> ); expect(spy).toHaveBeenCalled(); }); test('toggle success calls post', () => { const spy = jest.spyOn(_Notifications.prototype, 'createSuccess'); - const wrapper = mount( - - <_Notifications - match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }} - location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }} - onReadError={jest.fn()} - onReadNotifications={jest.fn()} - onReadSuccess={jest.fn()} - onCreateError={jest.fn()} - onCreateSuccess={jest.fn()} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + {}} + onReadNotifications={() => {}} + onReadSuccess={() => {}} + onCreateError={() => {}} + onCreateSuccess={() => {}} + /> ).find('Notifications'); wrapper.instance().toggleNotification(1, true, 'success'); expect(spy).toHaveBeenCalledWith(1, true); }); test('post success makes request and updates state properly', async () => { - const createSuccess = jest.fn(); - const wrapper = mount( - - - {}} - /> - - + const onCreateSuccess = jest.fn(); + const wrapper = mountWithContexts( + <_Notifications + match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }} + location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }} + handleHttpError={() => {}} + onReadError={() => {}} + onReadNotifications={() => {}} + onReadSuccess={() => {}} + onCreateError={() => {}} + onCreateSuccess={onCreateSuccess} + /> ).find('Notifications'); wrapper.setState({ successTemplateIds: [44] }); await wrapper.instance().createSuccess(44, true); - expect(createSuccess).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); + expect(onCreateSuccess).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); expect(wrapper.state('successTemplateIds')).not.toContain(44); await wrapper.instance().createSuccess(44, false); - expect(createSuccess).toHaveBeenCalledWith(1, { id: 44 }); + expect(onCreateSuccess).toHaveBeenCalledWith(1, { id: 44 }); expect(wrapper.state('successTemplateIds')).toContain(44); }); test('toggle error calls post', () => { const spy = jest.spyOn(_Notifications.prototype, 'createError'); - const wrapper = mount( - - <_Notifications - match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }} - location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }} - onReadError={jest.fn()} - onReadNotifications={jest.fn()} - onReadSuccess={jest.fn()} - onCreateError={jest.fn()} - onCreateSuccess={jest.fn()} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + {}} + onReadNotifications={() => {}} + onReadSuccess={() => {}} + onCreateError={() => {}} + onCreateSuccess={() => {}} + /> ).find('Notifications'); wrapper.instance().toggleNotification(1, true, 'error'); expect(spy).toHaveBeenCalledWith(1, true); }); test('post error makes request and updates state properly', async () => { - const createError = jest.fn(); - const wrapper = mount( - - - {}} - /> - - + const onCreateError = jest.fn(); + const wrapper = mountWithContexts( + <_Notifications + match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }} + location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }} + handleHttpError={() => {}} + onReadError={() => {}} + onReadNotifications={() => {}} + onReadSuccess={() => {}} + onCreateError={onCreateError} + onCreateSuccess={() => {}} + /> ).find('Notifications'); wrapper.setState({ errorTemplateIds: [44] }); await wrapper.instance().createError(44, true); - expect(createError).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); + expect(onCreateError).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); expect(wrapper.state('errorTemplateIds')).not.toContain(44); await wrapper.instance().createError(44, false); - expect(createError).toHaveBeenCalledWith(1, { id: 44 }); + expect(onCreateError).toHaveBeenCalledWith(1, { id: 44 }); expect(wrapper.state('errorTemplateIds')).toContain(44); }); @@ -159,28 +127,24 @@ describe('', () => { ] } }); - const readError = jest.fn().mockResolvedValue({ + const onReadError = jest.fn().mockResolvedValue({ data: { results: [ { id: 2 } ] } }); - const wrapper = mount( - - - {}} - /> - - + const wrapper = mountWithContexts( + <_Notifications + match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }} + location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }} + handleHttpError={() => {}} + onReadError={onReadError} + onReadNotifications={onReadNotifications} + onReadSuccess={onReadSuccess} + onCreateError={() => {}} + onCreateSuccess={() => {}} + /> ).find('Notifications'); wrapper.instance().updateUrl = jest.fn(); await wrapper.instance().readNotifications(mockQueryParams); @@ -188,7 +152,7 @@ describe('', () => { expect(onReadSuccess).toHaveBeenCalledWith(1, { id__in: '1,2,3' }); - expect(readError).toHaveBeenCalledWith(1, { + expect(onReadError).toHaveBeenCalledWith(1, { id__in: '1,2,3' }); expect(wrapper.state('successTemplateIds')).toContain(1); From 986641de9fb4d8a48b5de6b290a9c3259ee45115 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 22 Apr 2019 16:33:12 -0400 Subject: [PATCH 8/9] Fix ORganizationTeams test --- .../Organization/OrganizationTeams.test.jsx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx index 61b50bae57..76ab90af11 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationTeams.test.jsx @@ -1,5 +1,6 @@ import React from 'react'; import { shallow } from 'enzyme'; +import { Router } from 'react-router-dom'; import { createMemoryHistory } from 'history'; import { mountWithContexts } from '../../../../enzymeHelpers'; import { sleep } from '../../../../testUtils'; @@ -92,12 +93,18 @@ describe('', () => { initialEntries: ['/organizations/1/teams'], }); const wrapper = mountWithContexts( - , { context: { - network: { api: { readOrganizationTeamsList }, handleHttpError: () => {} }, - router: { history } + + + , + { context: { + network: { + api: { readOrganizationTeamsList }, + handleHttpError: () => {} + }, + router: false, } } ); From 261980f18e73182bbd0debf00973fdf655feb7ff Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 22 Apr 2019 16:34:33 -0400 Subject: [PATCH 9/9] update components tests to use mountWithContexts when relevant --- __tests__/components/About.test.jsx | 15 +- __tests__/components/AnsibleSelect.test.jsx | 59 ++-- __tests__/components/DataListToolbar.test.jsx | 193 ++++++------- __tests__/components/ExpandCollapse.test.jsx | 17 +- __tests__/components/Lookup.test.jsx | 264 ++++++++---------- .../components/NavExpandableGroup.test.jsx | 2 +- .../components/NotificationListItem.test.jsx | 102 +++---- .../components/NotifyAndRedirect.test.jsx | 21 +- .../components/PageHeaderToolbar.test.jsx | 31 +- __tests__/components/Search.test.jsx | 46 ++- __tests__/components/Sort.test.jsx | 148 +++++----- __tests__/components/TowerLogo.test.jsx | 47 +--- 12 files changed, 399 insertions(+), 546 deletions(-) diff --git a/__tests__/components/About.test.jsx b/__tests__/components/About.test.jsx index 86a4dab4c1..575265b9b7 100644 --- a/__tests__/components/About.test.jsx +++ b/__tests__/components/About.test.jsx @@ -1,6 +1,5 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../enzymeHelpers'; import About from '../../src/components/About'; describe('', () => { @@ -8,20 +7,16 @@ describe('', () => { let closeButton; const onClose = jest.fn(); test('initially renders without crashing', () => { - aboutWrapper = mount( - - - + aboutWrapper = mountWithContexts( + ); expect(aboutWrapper.length).toBe(1); aboutWrapper.unmount(); }); test('close button calls onClose handler', () => { - aboutWrapper = mount( - - - + aboutWrapper = mountWithContexts( + ); closeButton = aboutWrapper.find('AboutModalBoxCloseButton Button'); closeButton.simulate('click'); diff --git a/__tests__/components/AnsibleSelect.test.jsx b/__tests__/components/AnsibleSelect.test.jsx index d23d039d51..cd308f2cfc 100644 --- a/__tests__/components/AnsibleSelect.test.jsx +++ b/__tests__/components/AnsibleSelect.test.jsx @@ -1,37 +1,32 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../enzymeHelpers'; import AnsibleSelect from '../../src/components/AnsibleSelect'; const label = 'test select'; const mockData = ['/venv/baz/', '/venv/ansible/']; describe('', () => { test('initially renders succesfully', async () => { - mount( - - { }} - label={label} - data={mockData} - /> - + mountWithContexts( + { }} + label={label} + data={mockData} + /> ); }); test('calls "onSelectChange" on dropdown select change', () => { const spy = jest.spyOn(AnsibleSelect.prototype, 'onSelectChange'); - const wrapper = mount( - - { }} - label={label} - data={mockData} - /> - + const wrapper = mountWithContexts( + { }} + label={label} + data={mockData} + /> ); expect(spy).not.toHaveBeenCalled(); wrapper.find('select').simulate('change'); @@ -39,17 +34,15 @@ describe('', () => { }); test('Returns correct select options if defaultSelected props is passed', () => { - const wrapper = mount( - - { }} - label={label} - data={mockData} - defaultSelected={mockData[1]} - /> - + const wrapper = mountWithContexts( + { }} + label={label} + data={mockData} + defaultSelected={mockData[1]} + /> ); expect(wrapper.find('FormSelect')).toHaveLength(1); }); diff --git a/__tests__/components/DataListToolbar.test.jsx b/__tests__/components/DataListToolbar.test.jsx index 50e2a6f61b..a7ad38cca7 100644 --- a/__tests__/components/DataListToolbar.test.jsx +++ b/__tests__/components/DataListToolbar.test.jsx @@ -1,6 +1,5 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../enzymeHelpers'; import DataListToolbar from '../../src/components/DataListToolbar'; describe('', () => { @@ -25,20 +24,18 @@ describe('', () => { const onSort = jest.fn(); const onSelectAll = jest.fn(); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); toolbar.find(sort).simulate('click'); @@ -76,18 +73,16 @@ describe('', () => { const onSort = jest.fn(); const onSelectAll = jest.fn(); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); const sortDropdownToggle = toolbar.find(sortDropdownToggleSelector); expect(sortDropdownToggle.length).toBe(2); @@ -100,18 +95,16 @@ describe('', () => { const mockedSortEvent = { target: { innerText: 'Bar' } }; sortDropdownItems.at(0).simulate('click', mockedSortEvent); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); toolbar.update(); @@ -152,70 +145,62 @@ describe('', () => { const onSort = jest.fn(); const onSelectAll = jest.fn(); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); const downNumericIcon = toolbar.find(downNumericIconSelector); expect(downNumericIcon.length).toBe(1); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); const upNumericIcon = toolbar.find(upNumericIconSelector); expect(upNumericIcon.length).toBe(1); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); const downAlphaIcon = toolbar.find(downAlphaIconSelector); expect(downAlphaIcon.length).toBe(1); - toolbar = mount( - - - + toolbar = mountWithContexts( + ); const upAlphaIcon = toolbar.find(upAlphaIconSelector); @@ -232,22 +217,20 @@ describe('', () => { const showDelete = true; const disableTrashCanIcon = false; - toolbar = mount( - - [1, 2, 3, 4]} - sortedColumnKey="name" - sortOrder="ascending" - columns={columns} - onSearch={onSearch} - onSort={onSort} - onSelectAll={onSelectAll} - onOpenDeleteModal={onOpenDeleteModal} - showDelete={showDelete} - disableTrashCanIcon={disableTrashCanIcon} - /> - + toolbar = mountWithContexts( + [1, 2, 3, 4]} + sortedColumnKey="name" + sortOrder="ascending" + columns={columns} + onSearch={onSearch} + onSort={onSort} + onSelectAll={onSelectAll} + onOpenDeleteModal={onOpenDeleteModal} + showDelete={showDelete} + disableTrashCanIcon={disableTrashCanIcon} + /> ); toolbar.find(openDeleteModalButton).simulate('click'); diff --git a/__tests__/components/ExpandCollapse.test.jsx b/__tests__/components/ExpandCollapse.test.jsx index f2bf3e27aa..5e60e81a5e 100644 --- a/__tests__/components/ExpandCollapse.test.jsx +++ b/__tests__/components/ExpandCollapse.test.jsx @@ -1,6 +1,5 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../enzymeHelpers'; import ExpandCollapse from '../../src/components/ExpandCollapse'; describe('', () => { @@ -8,14 +7,12 @@ describe('', () => { const onExpand = jest.fn(); const isCompact = false; test('initially renders without crashing', () => { - const wrapper = mount( - - - + const wrapper = mountWithContexts( + ); expect(wrapper.length).toBe(1); wrapper.unmount(); diff --git a/__tests__/components/Lookup.test.jsx b/__tests__/components/Lookup.test.jsx index 7d08ee3c83..802b67082c 100644 --- a/__tests__/components/Lookup.test.jsx +++ b/__tests__/components/Lookup.test.jsx @@ -1,6 +1,5 @@ import React from 'react'; -import { mount } from 'enzyme'; -import { I18nProvider } from '@lingui/react'; +import { mountWithContexts } from '../enzymeHelpers'; import Lookup from '../../src/components/Lookup'; import { _Lookup } from '../../src/components/Lookup/Lookup'; @@ -10,36 +9,32 @@ const mockColumns = [ ]; describe('', () => { test('initially renders succesfully', () => { - mount( - - <_Lookup - lookup_header="Foo Bar" - name="fooBar" - value={mockData} - onLookupSave={() => { }} - getItems={() => { }} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + mountWithContexts( + <_Lookup + lookup_header="Foo Bar" + name="fooBar" + value={mockData} + onLookupSave={() => { }} + getItems={() => { }} + columns={mockColumns} + sortedColumnKey="name" + handleHttpError={() => {}} + /> ); }); test('API response is formatted properly', (done) => { - const wrapper = mount( - - <_Lookup - lookup_header="Foo Bar" - name="fooBar" - value={mockData} - onLookupSave={() => { }} - getItems={() => ({ data: { results: [{ name: 'test instance', id: 1 }] } })} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + lookup_header="Foo Bar" + name="fooBar" + value={mockData} + onLookupSave={() => { }} + getItems={() => ({ data: { results: [{ name: 'test instance', id: 1 }] } })} + columns={mockColumns} + sortedColumnKey="name" + handleHttpError={() => {}} + /> ).find('Lookup'); setImmediate(() => { @@ -51,20 +46,18 @@ describe('', () => { test('Opens modal when search icon is clicked', () => { const spy = jest.spyOn(_Lookup.prototype, 'handleModalToggle'); const mockSelected = [{ name: 'foo', id: 1 }]; - const wrapper = mount( - - <_Lookup - id="search" - lookup_header="Foo Bar" - name="fooBar" - value={mockSelected} - onLookupSave={() => { }} - getItems={() => { }} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + id="search" + lookup_header="Foo Bar" + name="fooBar" + value={mockSelected} + onLookupSave={() => { }} + getItems={() => { }} + columns={mockColumns} + sortedColumnKey="name" + handleHttpError={() => {}} + /> ).find('Lookup'); expect(spy).not.toHaveBeenCalled(); expect(wrapper.state('lookupSelectedItems')).toEqual(mockSelected); @@ -81,20 +74,18 @@ describe('', () => { test('calls "toggleSelected" when a user changes a checkbox', (done) => { const spy = jest.spyOn(_Lookup.prototype, 'toggleSelected'); const mockSelected = [{ name: 'foo', id: 1 }]; - const wrapper = mount( - - <_Lookup - id="search" - lookup_header="Foo Bar" - name="fooBar" - value={mockSelected} - onLookupSave={() => { }} - getItems={() => ({ data: { results: [{ name: 'test instance', id: 1 }] } })} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + id="search" + lookup_header="Foo Bar" + name="fooBar" + value={mockSelected} + onLookupSave={() => { }} + getItems={() => ({ data: { results: [{ name: 'test instance', id: 1 }] } })} + columns={mockColumns} + sortedColumnKey="name" + handleHttpError={() => {}} + /> ); setImmediate(() => { const searchItem = wrapper.find('button[aria-label="Search"]'); @@ -108,20 +99,18 @@ describe('', () => { test('calls "toggleSelected" when remove icon is clicked', () => { const spy = jest.spyOn(_Lookup.prototype, 'toggleSelected'); mockData = [{ name: 'foo', id: 1 }, { name: 'bar', id: 2 }]; - const wrapper = mount( - - <_Lookup - id="search" - lookup_header="Foo Bar" - name="fooBar" - value={mockData} - onLookupSave={() => { }} - getItems={() => { }} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + id="search" + lookup_header="Foo Bar" + name="fooBar" + value={mockData} + onLookupSave={() => { }} + getItems={() => { }} + columns={mockColumns} + sortedColumnKey="name" + handleHttpError={() => {}} + /> ); const removeIcon = wrapper.find('button[aria-label="close"]').first(); removeIcon.simulate('click'); @@ -130,19 +119,16 @@ describe('', () => { test('renders chips from prop value', () => { mockData = [{ name: 'foo', id: 0 }, { name: 'bar', id: 1 }]; - const wrapper = mount( - - { }} - value={mockData} - selected={[]} - getItems={() => { }} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + { }} + value={mockData} + selected={[]} + getItems={() => { }} + columns={mockColumns} + sortedColumnKey="name" + /> ).find('Lookup'); const chip = wrapper.find('li.pf-c-chip'); expect(chip).toHaveLength(2); @@ -150,18 +136,15 @@ describe('', () => { test('toggleSelected successfully adds/removes row from lookupSelectedItems state', () => { mockData = []; - const wrapper = mount( - - { }} - value={mockData} - getItems={() => { }} - columns={mockColumns} - sortedColumnKey="name" - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + { }} + value={mockData} + getItems={() => { }} + columns={mockColumns} + sortedColumnKey="name" + /> ).find('Lookup'); wrapper.instance().toggleSelected({ id: 1, @@ -181,17 +164,14 @@ describe('', () => { test('saveModal calls callback with selected items', () => { mockData = []; const onLookupSaveFn = jest.fn(); - const wrapper = mount( - - { }} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + { }} + /> ).find('Lookup'); wrapper.instance().toggleSelected({ id: 1, @@ -210,19 +190,17 @@ describe('', () => { test('onSort sets state and calls getData ', () => { const spy = jest.spyOn(_Lookup.prototype, 'getData'); - const wrapper = mount( - - <_Lookup - lookup_header="Foo Bar" - onLookupSave={() => { }} - value={mockData} - selected={[]} - columns={mockColumns} - sortedColumnKey="name" - getItems={() => { }} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + lookup_header="Foo Bar" + onLookupSave={() => { }} + value={mockData} + selected={[]} + columns={mockColumns} + sortedColumnKey="name" + getItems={() => { }} + handleHttpError={() => {}} + /> ).find('Lookup'); wrapper.instance().onSort('id', 'descending'); expect(wrapper.state('sortedColumnKey')).toEqual('id'); @@ -232,19 +210,17 @@ describe('', () => { test('onSearch calls getData (through calling onSort)', () => { const spy = jest.spyOn(_Lookup.prototype, 'getData'); - const wrapper = mount( - - <_Lookup - lookup_header="Foo Bar" - onLookupSave={() => { }} - value={mockData} - selected={[]} - columns={mockColumns} - sortedColumnKey="name" - getItems={() => { }} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + lookup_header="Foo Bar" + onLookupSave={() => { }} + value={mockData} + selected={[]} + columns={mockColumns} + sortedColumnKey="name" + getItems={() => { }} + handleHttpError={() => {}} + /> ).find('Lookup'); wrapper.instance().onSearch(); expect(spy).toHaveBeenCalled(); @@ -252,19 +228,17 @@ describe('', () => { test('onSetPage sets state and calls getData ', () => { const spy = jest.spyOn(_Lookup.prototype, 'getData'); - const wrapper = mount( - - <_Lookup - lookup_header="Foo Bar" - onLookupSave={() => { }} - value={mockData} - selected={[]} - columns={mockColumns} - sortedColumnKey="name" - getItems={() => { }} - handleHttpError={() => {}} - /> - + const wrapper = mountWithContexts( + <_Lookup + lookup_header="Foo Bar" + onLookupSave={() => { }} + value={mockData} + selected={[]} + columns={mockColumns} + sortedColumnKey="name" + getItems={() => { }} + handleHttpError={() => {}} + /> ).find('Lookup'); wrapper.instance().onSetPage(2, 10); expect(wrapper.state('page')).toEqual(2); diff --git a/__tests__/components/NavExpandableGroup.test.jsx b/__tests__/components/NavExpandableGroup.test.jsx index 7619dcbfcd..df803efea4 100644 --- a/__tests__/components/NavExpandableGroup.test.jsx +++ b/__tests__/components/NavExpandableGroup.test.jsx @@ -39,7 +39,7 @@ describe('NavExpandableGroup', () => { ]; params.forEach(([location, path, expected]) => { - test(`when location is ${location}', isActivePath('${path}') returns ${expected} `, () => { + test(`when location is ${location}, isActivePath('${path}') returns ${expected} `, () => { const component = mount(