|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails react-core |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +let React; |
| 13 | +let ReactDOM; |
| 14 | +let ReactIs; |
| 15 | + |
| 16 | +describe('ReactIs', () => { |
| 17 | + beforeEach(() => { |
| 18 | + jest.resetModules(); |
| 19 | + |
| 20 | + React = require('react'); |
| 21 | + ReactDOM = require('react-dom'); |
| 22 | + ReactIs = require('react-is'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return undefined for unknown/invalid types', () => { |
| 26 | + expect(ReactIs.typeOf('abc')).toBe(undefined); |
| 27 | + expect(ReactIs.typeOf(true)).toBe(undefined); |
| 28 | + expect(ReactIs.typeOf(123)).toBe(undefined); |
| 29 | + expect(ReactIs.typeOf({})).toBe(undefined); |
| 30 | + expect(ReactIs.typeOf(null)).toBe(undefined); |
| 31 | + expect(ReactIs.typeOf(undefined)).toBe(undefined); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should identify async mode', () => { |
| 35 | + expect(ReactIs.typeOf(<React.unstable_AsyncMode />)).toBe( |
| 36 | + ReactIs.AsyncMode, |
| 37 | + ); |
| 38 | + expect(ReactIs.isAsyncMode(<React.unstable_AsyncMode />)).toBe(true); |
| 39 | + expect(ReactIs.isAsyncMode({type: ReactIs.AsyncMode})).toBe(false); |
| 40 | + expect(ReactIs.isAsyncMode(<React.StrictMode />)).toBe(false); |
| 41 | + expect(ReactIs.isAsyncMode(<div />)).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should identify context consumers', () => { |
| 45 | + const Context = React.createContext(false); |
| 46 | + expect(ReactIs.typeOf(<Context.Consumer />)).toBe(ReactIs.ContextConsumer); |
| 47 | + expect(ReactIs.isContextConsumer(<Context.Consumer />)).toBe(true); |
| 48 | + expect(ReactIs.isContextConsumer(<Context.Provider />)).toBe(false); |
| 49 | + expect(ReactIs.isContextConsumer(<div />)).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should identify context providers', () => { |
| 53 | + const Context = React.createContext(false); |
| 54 | + expect(ReactIs.typeOf(<Context.Provider />)).toBe(ReactIs.ContextProvider); |
| 55 | + expect(ReactIs.isContextProvider(<Context.Provider />)).toBe(true); |
| 56 | + expect(ReactIs.isContextProvider(<Context.Consumer />)).toBe(false); |
| 57 | + expect(ReactIs.isContextProvider(<div />)).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should identify elements', () => { |
| 61 | + expect(ReactIs.typeOf(<div />)).toBe(ReactIs.Element); |
| 62 | + expect(ReactIs.isElement(<div />)).toBe(true); |
| 63 | + expect(ReactIs.isElement('div')).toBe(false); |
| 64 | + expect(ReactIs.isElement(true)).toBe(false); |
| 65 | + expect(ReactIs.isElement(123)).toBe(false); |
| 66 | + expect(ReactIs.isElement(null)).toBe(false); |
| 67 | + expect(ReactIs.isElement(undefined)).toBe(false); |
| 68 | + expect(ReactIs.isElement({})).toBe(false); |
| 69 | + |
| 70 | + // It should also identify more specific types as elements |
| 71 | + const Context = React.createContext(false); |
| 72 | + expect(ReactIs.isElement(<Context.Provider />)).toBe(true); |
| 73 | + expect(ReactIs.isElement(<Context.Consumer />)).toBe(true); |
| 74 | + expect(ReactIs.isElement(<React.Fragment />)).toBe(true); |
| 75 | + expect(ReactIs.isElement(<React.unstable_AsyncMode />)).toBe(true); |
| 76 | + expect(ReactIs.isElement(<React.StrictMode />)).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should identify fragments', () => { |
| 80 | + expect(ReactIs.typeOf(<React.Fragment />)).toBe(ReactIs.Fragment); |
| 81 | + expect(ReactIs.isFragment(<React.Fragment />)).toBe(true); |
| 82 | + expect(ReactIs.isFragment({type: ReactIs.Fragment})).toBe(false); |
| 83 | + expect(ReactIs.isFragment('React.Fragment')).toBe(false); |
| 84 | + expect(ReactIs.isFragment(<div />)).toBe(false); |
| 85 | + expect(ReactIs.isFragment([])).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should identify portals', () => { |
| 89 | + const div = document.createElement('div'); |
| 90 | + const portal = ReactDOM.createPortal(<div />, div); |
| 91 | + expect(ReactIs.typeOf(portal)).toBe(ReactIs.Portal); |
| 92 | + expect(ReactIs.isPortal(portal)).toBe(true); |
| 93 | + expect(ReactIs.isPortal(div)).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should identify strict mode', () => { |
| 97 | + expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode); |
| 98 | + expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true); |
| 99 | + expect(ReactIs.isStrictMode({type: ReactIs.StrictMode})).toBe(false); |
| 100 | + expect(ReactIs.isStrictMode(<React.unstable_AsyncMode />)).toBe(false); |
| 101 | + expect(ReactIs.isStrictMode(<div />)).toBe(false); |
| 102 | + }); |
| 103 | +}); |
0 commit comments