From ab971bab416943569e3036f77abbdfb684a961fc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 20 Oct 2023 00:22:32 +0300 Subject: [PATCH 1/2] Bump @babel/traverse from 7.11.0 to 7.23.2 (#83) Bumps [@babel/traverse](https://github.com/babel/babel/tree/HEAD/packages/babel-traverse) from 7.11.0 to 7.23.2. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.23.2/packages/babel-traverse) --- updated-dependencies: - dependency-name: "@babel/traverse" dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- yarn.lock | 164 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 134 insertions(+), 30 deletions(-) diff --git a/yarn.lock b/yarn.lock index 2f72bf3..027ec3e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -23,6 +23,14 @@ dependencies: "@babel/highlight" "^7.16.7" +"@babel/code-frame@^7.22.13": + version "7.22.13" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.22.13.tgz#e3c1c099402598483b7a8c46a721d1038803755e" + integrity sha512-XktuhWlJ5g+3TJXc5upd9Ks1HutSArik6jf2eAjYFyIOf4ej3RN+184cZbzDvbPnuTJIUhPKKJE3cIsYTiAT3w== + dependencies: + "@babel/highlight" "^7.22.13" + chalk "^2.4.2" + "@babel/compat-data@^7.10.4": version "7.11.0" resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.11.0.tgz#e9f73efe09af1355b723a7f39b11bad637d7c99c" @@ -80,7 +88,7 @@ json5 "^2.2.1" semver "^6.3.0" -"@babel/generator@^7.11.0", "@babel/generator@^7.11.4": +"@babel/generator@^7.11.4": version "7.11.4" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.11.4.tgz#1ec7eec00defba5d6f83e50e3ee72ae2fee482be" integrity sha512-Rn26vueFx0eOoz7iifCN2UHT6rGtnkSGWSoDRIy8jZN3B91PzeSULbswfLoOWuTuAcNwpG/mxy+uCTDnZ9Mp1g== @@ -98,6 +106,16 @@ jsesc "^2.5.1" source-map "^0.5.0" +"@babel/generator@^7.23.0": + version "7.23.0" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.23.0.tgz#df5c386e2218be505b34837acbcb874d7a983420" + integrity sha512-lN85QRR+5IbYrMWM6Y4pE/noaQtg4pNiqeNGX60eqOfo6gtEj6uw/JagelB8vVztSd7R6M5n1+PQkDbHbBRU4g== + dependencies: + "@babel/types" "^7.23.0" + "@jridgewell/gen-mapping" "^0.3.2" + "@jridgewell/trace-mapping" "^0.3.17" + jsesc "^2.5.1" + "@babel/helper-annotate-as-pure@^7.10.4": version "7.10.4" resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.10.4.tgz#5bf0d495a3f757ac3bda48b5bf3b3ba309c72ba3" @@ -218,6 +236,11 @@ dependencies: "@babel/types" "^7.16.7" +"@babel/helper-environment-visitor@^7.22.20": + version "7.22.20" + resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.20.tgz#96159db61d34a29dba454c959f5ae4a649ba9167" + integrity sha512-zfedSIzFhat/gFhWfHtgWvlec0nqB9YEIVrpuwjruLlXfUSnA8cJB0miHKwqDnQ7d32aKo2xt88/xZptwxbfhA== + "@babel/helper-explode-assignable-expression@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/helper-explode-assignable-expression/-/helper-explode-assignable-expression-7.16.7.tgz#12a6d8522fdd834f194e868af6354e8650242b7a" @@ -242,6 +265,14 @@ "@babel/template" "^7.16.7" "@babel/types" "^7.17.0" +"@babel/helper-function-name@^7.23.0": + version "7.23.0" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.23.0.tgz#1f9a3cdbd5b2698a670c30d2735f9af95ed52759" + integrity sha512-OErEqsrxjZTJciZ4Oo+eoZqeW9UIiOcuYKRJA4ZAgV9myA+pOXhhmpfNCKjEH/auVfEYVFJ6y1Tc4r0eIApqiw== + dependencies: + "@babel/template" "^7.22.15" + "@babel/types" "^7.23.0" + "@babel/helper-get-function-arity@^7.10.4": version "7.10.4" resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.10.4.tgz#98c1cbea0e2332f33f9a4661b8ce1505b2c19ba2" @@ -256,6 +287,13 @@ dependencies: "@babel/types" "^7.16.7" +"@babel/helper-hoist-variables@^7.22.5": + version "7.22.5" + resolved "https://registry.yarnpkg.com/@babel/helper-hoist-variables/-/helper-hoist-variables-7.22.5.tgz#c01a007dac05c085914e8fb652b339db50d823bb" + integrity sha512-wGjk9QZVzvknA6yKIUURb8zY3grXCcOZt+/7Wcy8O2uctxhplmUPkOdlgoNhmdVee2c92JXbf1xpMtVNbfoxRw== + dependencies: + "@babel/types" "^7.22.5" + "@babel/helper-member-expression-to-functions@^7.10.4", "@babel/helper-member-expression-to-functions@^7.10.5": version "7.11.0" resolved "https://registry.yarnpkg.com/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.11.0.tgz#ae69c83d84ee82f4b42f96e2a09410935a8f26df" @@ -408,6 +446,18 @@ dependencies: "@babel/types" "^7.16.7" +"@babel/helper-split-export-declaration@^7.22.6": + version "7.22.6" + resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.22.6.tgz#322c61b7310c0997fe4c323955667f18fcefb91c" + integrity sha512-AsUnxuLhRYsisFiaJwvp1QF+I3KjD5FOxut14q/GzovUe6orHLesW2C7d754kRm53h5gqrz6sFl6sxc4BVtE/g== + dependencies: + "@babel/types" "^7.22.5" + +"@babel/helper-string-parser@^7.22.5": + version "7.22.5" + resolved "https://registry.yarnpkg.com/@babel/helper-string-parser/-/helper-string-parser-7.22.5.tgz#533f36457a25814cf1df6488523ad547d784a99f" + integrity sha512-mM4COjgZox8U+JcXQwPijIZLElkgEpO5rsERVDJTc2qfCDfERyob6k5WegS14SX18IIjv+XD+GrqNumY5JRCDw== + "@babel/helper-validator-identifier@^7.10.4": version "7.10.4" resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.10.4.tgz#a78c7a7251e01f616512d31b10adcf52ada5e0d2" @@ -418,6 +468,11 @@ resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.16.7.tgz#e8c602438c4a8195751243da9031d1607d247cad" integrity sha512-hsEnFemeiW4D08A5gUAZxLBTXpZ39P+a+DGDsHw1yxqyQ/jzFEnxf5uTEGp+3bzAbNOxU1paTgYS4ECU/IgfDw== +"@babel/helper-validator-identifier@^7.22.20": + version "7.22.20" + resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.20.tgz#c4ae002c61d2879e724581d96665583dbc1dc0e0" + integrity sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A== + "@babel/helper-validator-option@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/helper-validator-option/-/helper-validator-option-7.16.7.tgz#b203ce62ce5fe153899b617c08957de860de4d23" @@ -469,7 +524,16 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.11.0", "@babel/parser@^7.11.4", "@babel/parser@^7.7.0": +"@babel/highlight@^7.22.13": + version "7.22.20" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.22.20.tgz#4ca92b71d80554b01427815e06f2df965b9c1f54" + integrity sha512-dkdMCN3py0+ksCgYmGG8jKeGA/8Tk+gJwSYYlFGxG5lmhfKNoAy004YpLxpS1W2J8m/EK2Ew+yOs9pVRwO89mg== + dependencies: + "@babel/helper-validator-identifier" "^7.22.20" + chalk "^2.4.2" + js-tokens "^4.0.0" + +"@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.11.4", "@babel/parser@^7.7.0": version "7.11.4" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.11.4.tgz#6fa1a118b8b0d80d0267b719213dc947e88cc0ca" integrity sha512-MggwidiH+E9j5Sh8pbrX5sJvMcsqS5o+7iB42M9/k0CD63MjYbdP4nhSh7uB5wnv2/RVzTZFTxzF/kIa5mrCqA== @@ -479,6 +543,11 @@ resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.17.9.tgz#9c94189a6062f0291418ca021077983058e171ef" integrity sha512-vqUSBLP8dQHFPdPi9bc5GK9vRkYHJ49fsZdtoJ8EQ8ibpwk5rPKfvNIwChB0KVXcIjcepEBBd2VHC5r9Gy8ueg== +"@babel/parser@^7.22.15", "@babel/parser@^7.23.0": + version "7.23.0" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.23.0.tgz#da950e622420bf96ca0d0f2909cdddac3acd8719" + integrity sha512-vvPKKdMemU85V9WE/l5wZEmImpCtLqbnTvqDS2U1fJ96KrxoW7KrXhNsNCblQlg8Ck4b85yxdTyelsMUgFUXiw== + "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression/-/plugin-bugfix-safari-id-destructuring-collision-in-function-expression-7.16.7.tgz#4eda6d6c2a0aa79c70fa7b6da67763dfe2141050" @@ -1147,34 +1216,28 @@ "@babel/parser" "^7.16.7" "@babel/types" "^7.16.7" -"@babel/traverse@^7.1.0", "@babel/traverse@^7.10.4", "@babel/traverse@^7.11.0", "@babel/traverse@^7.7.0": - version "7.11.0" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.11.0.tgz#9b996ce1b98f53f7c3e4175115605d56ed07dd24" - integrity sha512-ZB2V+LskoWKNpMq6E5UUCrjtDUh5IOTAyIl0dTjIEoXum/iKWkoIEKIRDnUucO6f+2FzNkE0oD4RLKoPIufDtg== - dependencies: - "@babel/code-frame" "^7.10.4" - "@babel/generator" "^7.11.0" - "@babel/helper-function-name" "^7.10.4" - "@babel/helper-split-export-declaration" "^7.11.0" - "@babel/parser" "^7.11.0" - "@babel/types" "^7.11.0" - debug "^4.1.0" - globals "^11.1.0" - lodash "^4.17.19" - -"@babel/traverse@^7.11.5", "@babel/traverse@^7.13.0", "@babel/traverse@^7.16.7", "@babel/traverse@^7.16.8", "@babel/traverse@^7.17.3", "@babel/traverse@^7.17.9": - version "7.17.9" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.17.9.tgz#1f9b207435d9ae4a8ed6998b2b82300d83c37a0d" - integrity sha512-PQO8sDIJ8SIwipTPiR71kJQCKQYB5NGImbOviK8K+kg5xkNSYXLBupuX9QhatFowrsvo9Hj8WgArg3W7ijNAQw== - dependencies: - "@babel/code-frame" "^7.16.7" - "@babel/generator" "^7.17.9" - "@babel/helper-environment-visitor" "^7.16.7" - "@babel/helper-function-name" "^7.17.9" - "@babel/helper-hoist-variables" "^7.16.7" - "@babel/helper-split-export-declaration" "^7.16.7" - "@babel/parser" "^7.17.9" - "@babel/types" "^7.17.0" +"@babel/template@^7.22.15": + version "7.22.15" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.22.15.tgz#09576efc3830f0430f4548ef971dde1350ef2f38" + integrity sha512-QPErUVm4uyJa60rkI73qneDacvdvzxshT3kksGqlGWYdOTIUOwJ7RDUL8sGqslY1uXWSL6xMFKEXDS3ox2uF0w== + dependencies: + "@babel/code-frame" "^7.22.13" + "@babel/parser" "^7.22.15" + "@babel/types" "^7.22.15" + +"@babel/traverse@^7.1.0", "@babel/traverse@^7.10.4", "@babel/traverse@^7.11.0", "@babel/traverse@^7.11.5", "@babel/traverse@^7.13.0", "@babel/traverse@^7.16.7", "@babel/traverse@^7.16.8", "@babel/traverse@^7.17.3", "@babel/traverse@^7.17.9", "@babel/traverse@^7.7.0": + version "7.23.2" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.2.tgz#329c7a06735e144a506bdb2cad0268b7f46f4ad8" + integrity sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw== + dependencies: + "@babel/code-frame" "^7.22.13" + "@babel/generator" "^7.23.0" + "@babel/helper-environment-visitor" "^7.22.20" + "@babel/helper-function-name" "^7.23.0" + "@babel/helper-hoist-variables" "^7.22.5" + "@babel/helper-split-export-declaration" "^7.22.6" + "@babel/parser" "^7.23.0" + "@babel/types" "^7.23.0" debug "^4.1.0" globals "^11.1.0" @@ -1195,6 +1258,15 @@ "@babel/helper-validator-identifier" "^7.16.7" to-fast-properties "^2.0.0" +"@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0": + version "7.23.0" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.0.tgz#8c1f020c9df0e737e4e247c0619f58c68458aaeb" + integrity sha512-0oIyUfKoI3mSqMvsxBdclDwxXKXAUA8v/apZbc+iSyARYou1o8ZGDxbUYyLFoW2arqS2jDGqJuZvv1d/io1axg== + dependencies: + "@babel/helper-string-parser" "^7.22.5" + "@babel/helper-validator-identifier" "^7.22.20" + to-fast-properties "^2.0.0" + "@bcoe/v8-coverage@^0.2.3": version "0.2.3" resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" @@ -1409,16 +1481,40 @@ "@types/yargs" "^15.0.0" chalk "^3.0.0" +"@jridgewell/gen-mapping@^0.3.2": + version "0.3.3" + resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.3.tgz#7e02e6eb5df901aaedb08514203b096614024098" + integrity sha512-HLhSWOLRi875zjjMG/r+Nv0oCW8umGb0BgEhyX3dDX3egwZtB8PqLnjz3yedt8R5StBrzcg4aBpnh8UA9D1BoQ== + dependencies: + "@jridgewell/set-array" "^1.0.1" + "@jridgewell/sourcemap-codec" "^1.4.10" + "@jridgewell/trace-mapping" "^0.3.9" + "@jridgewell/resolve-uri@^3.0.3": version "3.0.5" resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.0.5.tgz#68eb521368db76d040a6315cdb24bf2483037b9c" integrity sha512-VPeQ7+wH0itvQxnG+lIzWgkysKIr3L9sslimFW55rHMdGu/qCQ5z5h9zq4gI8uBtqkpHhsF4Z/OwExufUCThew== +"@jridgewell/resolve-uri@^3.1.0": + version "3.1.1" + resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz#c08679063f279615a3326583ba3a90d1d82cc721" + integrity sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA== + +"@jridgewell/set-array@^1.0.1": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.2.tgz#7c6cf998d6d20b914c0a55a91ae928ff25965e72" + integrity sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw== + "@jridgewell/sourcemap-codec@^1.4.10": version "1.4.11" resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.11.tgz#771a1d8d744eeb71b6adb35808e1a6c7b9b8c8ec" integrity sha512-Fg32GrJo61m+VqYSdRSjRXMjQ06j8YIYfcTqndLYVAaHmroZHLJZCydsWBOTDqXS2v+mjxohBWEMfg97GXmYQg== +"@jridgewell/sourcemap-codec@^1.4.14": + version "1.4.15" + resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz#d7c6e6755c78567a951e04ab52ef0fd26de59f32" + integrity sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg== + "@jridgewell/trace-mapping@^0.3.0": version "0.3.9" resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9" @@ -1427,6 +1523,14 @@ "@jridgewell/resolve-uri" "^3.0.3" "@jridgewell/sourcemap-codec" "^1.4.10" +"@jridgewell/trace-mapping@^0.3.17", "@jridgewell/trace-mapping@^0.3.9": + version "0.3.19" + resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.19.tgz#f8a3249862f91be48d3127c3cfe992f79b4b8811" + integrity sha512-kf37QtfW+Hwx/buWGMPcR60iF9ziHa6r/CZJIHbmcm4+0qrXiVdxegAH0F6yddEVQ7zdkjcGCgCzUu+BcbhQxw== + dependencies: + "@jridgewell/resolve-uri" "^3.1.0" + "@jridgewell/sourcemap-codec" "^1.4.14" + "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" From 0061f8ab3f5fdb4ecc9b337f583bcfb6d9f4db3e Mon Sep 17 00:00:00 2001 From: Anton <21086792+Quernest@users.noreply.github.com> Date: Fri, 20 Oct 2023 00:23:09 +0300 Subject: [PATCH 2/2] #84 added throw an error for missed context provider (#85) * #84 added throw an error for missed context provider * #84 fixed unit tests * #84 patch version --- package.json | 2 +- src/modal-context.test.tsx | 16 +++++++++++----- src/modal-context.ts | 17 +---------------- src/modal-provider.test.tsx | 18 ++++++++---------- src/test-utils/index.tsx | 8 -------- src/use-modal-context.test.tsx | 22 ++++++++++++++++++++++ src/use-modal-context.ts | 12 ++++++++++++ src/use-modal.ts | 7 +++---- 8 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 src/use-modal-context.test.tsx create mode 100644 src/use-modal-context.ts diff --git a/package.json b/package.json index 975e398..c07bb07 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "2.2.1", + "version": "2.3.0", "license": "MIT", "name": "mui-modal-provider", "author": "Quernest", diff --git a/src/modal-context.test.tsx b/src/modal-context.test.tsx index 9d97c0d..207d241 100644 --- a/src/modal-context.test.tsx +++ b/src/modal-context.test.tsx @@ -1,19 +1,25 @@ import React from 'react'; import { act, renderHook } from '@testing-library/react-hooks'; - -import ModalContext, { initialContextState } from './modal-context'; -import { ModalContextProviderWrapper as wrapper } from './test-utils'; +import { ModalProviderWrapper as wrapper } from './test-utils'; +import useModalContext from './use-modal-context'; describe('ModalContext', () => { const rootId = '123'; const modalId = '321'; test('should be initialized with correct state', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper, }); - expect(result.current).toMatchObject(initialContextState); + expect(result.current).toMatchObject({ + destroyModal: expect.any(Function), + destroyModalsByRootId: expect.any(Function), + hideModal: expect.any(Function), + showModal: expect.any(Function), + state: {}, + updateModal: expect.any(Function), + }); act(() => { const modal = result.current.showModal(() =>
test
); diff --git a/src/modal-context.ts b/src/modal-context.ts index 64ccc1f..84cf280 100644 --- a/src/modal-context.ts +++ b/src/modal-context.ts @@ -1,5 +1,4 @@ import { createContext } from 'react'; -import { initialState } from './reducer'; import { State, HideFn, @@ -18,20 +17,6 @@ export interface ModalContextState { showModal: ShowFn; } -export const initialContextState = { - state: initialState, - hideModal: () => {}, - showModal: () => ({ - id: 'id', - hide: () => {}, - destroy: () => {}, - update: () => {}, - }), - destroyModal: () => {}, - updateModal: () => {}, - destroyModalsByRootId: () => {}, -}; - -const ModalContext = createContext(initialContextState); +const ModalContext = createContext(undefined); export default ModalContext; diff --git a/src/modal-provider.test.tsx b/src/modal-provider.test.tsx index 46a8647..c81f9b0 100644 --- a/src/modal-provider.test.tsx +++ b/src/modal-provider.test.tsx @@ -1,8 +1,6 @@ -import React from 'react'; import { act, renderHook } from '@testing-library/react-hooks'; - import * as utils from './utils'; -import ModalContext from './modal-context'; +import useModalContext from './use-modal-context'; import { LegacyModalProviderWrapper as legacyWrapper, ModalProviderWrapper as wrapper, @@ -45,7 +43,7 @@ describe('ModalProvider', () => { }); test('happy path scenario (with options)', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper, }); @@ -88,7 +86,7 @@ describe('ModalProvider', () => { }); test('unhappy path (missed ID errors)', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper, }); @@ -126,7 +124,7 @@ describe('ModalProvider', () => { }); test('happy path scenario (without options)', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper, }); @@ -148,7 +146,7 @@ describe('ModalProvider', () => { }); it('should automaticaly destroy on close', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper, }); @@ -165,7 +163,7 @@ describe('ModalProvider', () => { }); it('should fire onClose prop event on hide', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper, }); @@ -186,7 +184,7 @@ describe('ModalProvider', () => { }); it('should fire TransitionProps.onExited prop event on hide', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper: noSuspenseWrapper, }); @@ -207,7 +205,7 @@ describe('ModalProvider', () => { }); it('should fire onExited prop event on hide', () => { - const { result } = renderHook(() => React.useContext(ModalContext), { + const { result } = renderHook(() => useModalContext(), { wrapper: legacyWrapper, }); diff --git a/src/test-utils/index.tsx b/src/test-utils/index.tsx index 9a26ecc..166b260 100644 --- a/src/test-utils/index.tsx +++ b/src/test-utils/index.tsx @@ -1,6 +1,4 @@ import React, { ReactNode, FC } from 'react'; - -import ModalContext, { initialContextState } from '../modal-context'; import ModalProvider from '../modal-provider'; export const OnCloseEvent = new Event('close'); @@ -21,9 +19,3 @@ export const LegacyModalProviderWrapper: FC = ({ children }) => ( export const NoSuspenseModalProviderWrapper: FC = ({ children }) => ( {children} ); - -export const ModalContextProviderWrapper: FC = ({ children }) => ( - - {children} - -); diff --git a/src/use-modal-context.test.tsx b/src/use-modal-context.test.tsx new file mode 100644 index 0000000..91c022a --- /dev/null +++ b/src/use-modal-context.test.tsx @@ -0,0 +1,22 @@ +import { useContext } from 'react'; +import useModalContext from './use-modal-context'; + +jest.mock('react', () => ({ + ...jest.requireActual('react'), + useContext: jest.fn(), +})); + +describe('useModalContext', () => { + it('throws an error when not used within ModalProvider', () => { + (useContext as jest.Mock).mockReturnValue(undefined); + expect(() => useModalContext()).toThrow( + 'useModalContext must be used within a ModalProvider' + ); + }); + + it('returns the context when used within ModalProvider', () => { + const mockContextValue = { someValue: 'mockValue' }; + (useContext as jest.Mock).mockReturnValue(mockContextValue); + expect(useModalContext()).toBe(mockContextValue); + }); +}); diff --git a/src/use-modal-context.ts b/src/use-modal-context.ts new file mode 100644 index 0000000..c54b63a --- /dev/null +++ b/src/use-modal-context.ts @@ -0,0 +1,12 @@ +import { useContext } from 'react'; +import ModalContext from './modal-context'; + +export default function useModalContext() { + const context = useContext(ModalContext); + + if (context === undefined) { + throw new Error('useModalContext must be used within a ModalProvider'); + } + + return context; +} diff --git a/src/use-modal.ts b/src/use-modal.ts index 1c0f97f..de8e080 100644 --- a/src/use-modal.ts +++ b/src/use-modal.ts @@ -1,8 +1,7 @@ -import { useContext, useCallback, useEffect, useRef } from 'react'; +import { useCallback, useEffect, useRef } from 'react'; import { ShowFn } from './types'; - -import ModalContext from './modal-context'; import { uid } from './utils'; +import useModalContext from './use-modal-context'; export interface UseModalOptions { disableAutoDestroy?: boolean; @@ -18,7 +17,7 @@ export default function useModal(options: UseModalOptions = defaultOptions) { showModal, destroyModalsByRootId: destroy, ...otherContextProps - } = useContext(ModalContext); + } = useModalContext(); const id = useRef(uid(6)); useEffect(