|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @format |
| 10 | + */ |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +describe('checkVersion', () => { |
| 14 | + describe('in development', () => { |
| 15 | + _setDevelopmentModeForTests(true); |
| 16 | + _defineCheckVersionTests(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('in production', () => { |
| 20 | + _setDevelopmentModeForTests(false); |
| 21 | + _defineCheckVersionTests(); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +function _setDevelopmentModeForTests(dev) { |
| 26 | + let originalDev; |
| 27 | + |
| 28 | + beforeAll(() => { |
| 29 | + originalDev = global.__DEV__; |
| 30 | + global.__DEV__ = dev; |
| 31 | + }); |
| 32 | + |
| 33 | + afterAll(() => { |
| 34 | + global.__DEV__ = originalDev; |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function _defineCheckVersionTests() { |
| 39 | + afterEach(() => { |
| 40 | + jest.resetModules(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('passes when all the versions are zero', () => { |
| 44 | + jest.dontMock('ReactNativeVersion'); |
| 45 | + _mockNativeVersion(0, 0, 0); |
| 46 | + |
| 47 | + const ReactNativeVersion = require('ReactNativeVersion'); |
| 48 | + const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); |
| 49 | + expect(ReactNativeVersion).toMatchObject({ |
| 50 | + version: {major: 0, minor: 0, patch: 0, prerelease: null}, |
| 51 | + }); |
| 52 | + expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('passes when the minor matches when the major is zero', () => { |
| 56 | + _mockJsVersion(0, 1, 0); |
| 57 | + _mockNativeVersion(0, 1, 0); |
| 58 | + |
| 59 | + const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); |
| 60 | + expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("throws when the minor doesn't match when the major is zero", () => { |
| 64 | + _mockJsVersion(0, 1, 0); |
| 65 | + _mockNativeVersion(0, 2, 0); |
| 66 | + |
| 67 | + const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); |
| 68 | + expect(() => ReactNativeVersionCheck.checkVersions()).toThrowError( |
| 69 | + /React Native version mismatch/, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("throws when the major doesn't match", () => { |
| 74 | + _mockJsVersion(1, 0, 0); |
| 75 | + _mockNativeVersion(2, 0, 0); |
| 76 | + |
| 77 | + const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); |
| 78 | + expect(() => ReactNativeVersionCheck.checkVersions()).toThrowError( |
| 79 | + /React Native version mismatch/, |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("doesn't throw if the patch doesn't match", () => { |
| 84 | + _mockJsVersion(0, 1, 0); |
| 85 | + _mockNativeVersion(0, 1, 2); |
| 86 | + |
| 87 | + const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); |
| 88 | + expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("doesn't throw if the prerelease doesn't match", () => { |
| 92 | + _mockJsVersion(0, 1, 0, 'beta.0'); |
| 93 | + _mockNativeVersion(0, 1, 0, 'alpha.1'); |
| 94 | + |
| 95 | + const ReactNativeVersionCheck = require('ReactNativeVersionCheck'); |
| 96 | + expect(() => ReactNativeVersionCheck.checkVersions()).not.toThrow(); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function _mockJsVersion(major = 0, minor = 0, patch = 0, prerelease = null) { |
| 101 | + jest.doMock('ReactNativeVersion', () => ({ |
| 102 | + version: {major, minor, patch, prerelease}, |
| 103 | + })); |
| 104 | +} |
| 105 | + |
| 106 | +function _mockNativeVersion( |
| 107 | + major = 0, |
| 108 | + minor = 0, |
| 109 | + patch = 0, |
| 110 | + prerelease = null, |
| 111 | +) { |
| 112 | + jest.doMock('NativeModules', () => ({ |
| 113 | + PlatformConstants: { |
| 114 | + reactNativeVersion: {major, minor, patch, prerelease}, |
| 115 | + }, |
| 116 | + })); |
| 117 | +} |
0 commit comments