@@ -35,7 +35,7 @@ describe('inline constants', () => {
3535 var a = __DEV__ ? 1 : 2;
3636 var b = a.__DEV__;
3737 var c = function __DEV__(__DEV__) {};
38- }`
38+ }` ;
3939 const { ast} = inline ( 'arbitrary.js' , { code} , { dev : true } ) ;
4040 expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / _ _ D E V _ _ / , 'true' ) ) ) ;
4141 } ) ;
@@ -44,7 +44,7 @@ describe('inline constants', () => {
4444 const code = `function a() {
4545 var a = Platform.OS;
4646 var b = a.Platform.OS;
47- }`
47+ }` ;
4848 const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
4949 expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / P l a t f o r m \. O S / , '"ios"' ) ) ) ;
5050 } ) ;
@@ -55,7 +55,18 @@ describe('inline constants', () => {
5555 function a() {
5656 if (Platform.OS === 'android') a = function() {};
5757 var b = a.Platform.OS;
58- }`
58+ }` ;
59+ const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
60+ expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / P l a t f o r m \. O S / , '"ios"' ) ) ) ;
61+ } ) ;
62+
63+ it ( 'replaces Platform.OS in the code if Platform is a top level import from react-native' , ( ) => {
64+ const code = `
65+ var Platform = require('react-native').Platform;
66+ function a() {
67+ if (Platform.OS === 'android') a = function() {};
68+ var b = a.Platform.OS;
69+ }` ;
5970 const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
6071 expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / P l a t f o r m \. O S / , '"ios"' ) ) ) ;
6172 } ) ;
@@ -64,7 +75,7 @@ describe('inline constants', () => {
6475 const code = `function a() {
6576 var a = require('Platform').OS;
6677 var b = a.require('Platform').OS;
67- }`
78+ }` ;
6879 const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'android' } ) ;
6980 expect ( toString ( ast ) ) . toEqual (
7081 normalize ( code . replace ( / r e q u i r e \( ' P l a t f o r m ' \) \. O S / , '"android"' ) ) ) ;
@@ -74,18 +85,27 @@ describe('inline constants', () => {
7485 const code = `function a() {
7586 var a = React.Platform.OS;
7687 var b = a.React.Platform.OS;
77- }`
88+ }` ;
7889 const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
7990 expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / R e a c t \. P l a t f o r m \. O S / , '"ios"' ) ) ) ;
8091 } ) ;
8192
93+ it ( 'replaces ReactNative.Platform.OS in the code if ReactNative is a global' , ( ) => {
94+ const code = `function a() {
95+ var a = ReactNative.Platform.OS;
96+ var b = a.ReactNative.Platform.OS;
97+ }` ;
98+ const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
99+ expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / R e a c t N a t i v e \. P l a t f o r m \. O S / , '"ios"' ) ) ) ;
100+ } ) ;
101+
82102 it ( 'replaces React.Platform.OS in the code if React is a top level import' , ( ) => {
83103 const code = `
84104 var React = require('React');
85105 function a() {
86106 if (React.Platform.OS === 'android') a = function() {};
87107 var b = a.React.Platform.OS;
88- }`
108+ }` ;
89109 const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
90110 expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / R e a c t .P l a t f o r m \. O S / , '"ios"' ) ) ) ;
91111 } ) ;
@@ -94,19 +114,40 @@ describe('inline constants', () => {
94114 const code = `function a() {
95115 var a = require('React').Platform.OS;
96116 var b = a.require('React').Platform.OS;
97- }`
117+ }` ;
98118 const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'android' } ) ;
99119 expect ( toString ( ast ) ) . toEqual (
100120 normalize ( code . replace ( / r e q u i r e \( ' R e a c t ' \) \. P l a t f o r m \. O S / , '"android"' ) ) ) ;
101121 } ) ;
102122
123+ it ( 'replaces ReactNative.Platform.OS in the code if ReactNative is a top level import' , ( ) => {
124+ const code = `
125+ var ReactNative = require('react-native');
126+ function a() {
127+ if (ReactNative.Platform.OS === 'android') a = function() {};
128+ var b = a.ReactNative.Platform.OS;
129+ }` ;
130+ const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'android' } ) ;
131+ expect ( toString ( ast ) ) . toEqual ( normalize ( code . replace ( / R e a c t N a t i v e .P l a t f o r m \. O S / , '"android"' ) ) ) ;
132+ } ) ;
133+
134+ it ( 'replaces require("react-native").Platform.OS in the code' , ( ) => {
135+ const code = `function a() {
136+ var a = require('react-native').Platform.OS;
137+ var b = a.require('react-native').Platform.OS;
138+ }` ;
139+ const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'android' } ) ;
140+ expect ( toString ( ast ) ) . toEqual (
141+ normalize ( code . replace ( / r e q u i r e \( ' r e a c t - n a t i v e ' \) \. P l a t f o r m \. O S / , '"android"' ) ) ) ;
142+ } ) ;
143+
103144 it ( 'replaces process.env.NODE_ENV in the code' , ( ) => {
104145 const code = `function a() {
105146 if (process.env.NODE_ENV === 'production') {
106147 return require('Prod');
107148 }
108149 return require('Dev');
109- }`
150+ }` ;
110151 const { ast} = inline ( 'arbitrary.js' , { code} , { dev : false } ) ;
111152 expect ( toString ( ast ) ) . toEqual (
112153 normalize ( code . replace ( / p r o c e s s \. e n v \. N O D E _ E N V / , '"production"' ) ) ) ;
@@ -118,16 +159,28 @@ describe('inline constants', () => {
118159 return require('Prod');
119160 }
120161 return require('Dev');
121- }`
162+ }` ;
122163 const { ast} = inline ( 'arbitrary.js' , { code} , { dev : true } ) ;
123164 expect ( toString ( ast ) ) . toEqual (
124165 normalize ( code . replace ( / p r o c e s s \. e n v \. N O D E _ E N V / , '"development"' ) ) ) ;
125166 } ) ;
126167
168+ it ( 'replaces process.platform in the code' , ( ) => {
169+ const code = `function a() {
170+ if (process.platform === 'android') {
171+ return require('./android');
172+ }
173+ return require('./ios');
174+ }` ;
175+ const { ast} = inline ( 'arbitrary.js' , { code} , { platform : 'ios' } ) ;
176+ expect ( toString ( ast ) ) . toEqual (
177+ normalize ( code . replace ( / p r o c e s s \. p l a t f o r m \b / , '"ios"' ) ) ) ;
178+ } ) ;
179+
127180 it ( 'accepts an AST as input' , function ( ) {
128- const code = ` function ifDev(a,b){return __DEV__?a:b;}` ;
181+ const code = ' function ifDev(a,b){return __DEV__?a:b;}' ;
129182 const { ast} = inline ( 'arbitrary.hs' , { ast : toAst ( code ) } , { dev : false } ) ;
130- expect ( toString ( ast ) ) . toEqual ( code . replace ( / _ _ D E V _ _ / , 'false' ) )
183+ expect ( toString ( ast ) ) . toEqual ( code . replace ( / _ _ D E V _ _ / , 'false' ) ) ;
131184 } ) ;
132185} ) ;
133186
0 commit comments