|
5 | 5 |
|
6 | 6 | RSpec.describe InjectableEnv do
|
7 | 7 |
|
| 8 | + Placeholder = '{{REACT_APP_VARS_AS_JSON______________________________________________________________________________________________________}}' |
| 9 | + UnpaddedPlaceholder = '{{REACT_APP_VARS_AS_JSON}}' |
| 10 | + |
8 | 11 | describe '.create' do
|
9 | 12 | it "returns empty object" do
|
10 | 13 | expect(InjectableEnv.create).to eq('{}')
|
|
63 | 66 |
|
64 | 67 | describe '.replace' do
|
65 | 68 | before do
|
66 |
| - ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today" |
| 69 | + ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today 🌞" |
67 | 70 | end
|
68 | 71 | after do
|
69 | 72 | ENV.delete 'REACT_APP_HELLO'
|
|
72 | 75 | it "writes into file" do
|
73 | 76 | begin
|
74 | 77 | file = Tempfile.new('injectable_env_test')
|
75 |
| - file.write('var injected="{{REACT_APP_VARS_AS_JSON}}"') |
| 78 | + file.write(%{var injected="#{Placeholder}"}) |
76 | 79 | file.rewind
|
77 | 80 |
|
78 | 81 | InjectableEnv.replace(file.path)
|
79 | 82 |
|
80 |
| - expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today\\"}"' |
| 83 | + expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}' |
81 | 84 | actual_value=file.read
|
82 |
| - expect(actual_value).to eq(expected_value) |
| 85 | + expect(actual_value.index(expected_value)).to eq(0) |
| 86 | + # Closing double-quote is padded out but still last char. |
| 87 | + actual_size = actual_value.size |
| 88 | + expect(actual_value.index(/\"\Z/)).to eq(actual_size-1) |
| 89 | + ensure |
| 90 | + if file |
| 91 | + file.close |
| 92 | + file.unlink |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + it "matches unpadded placeholder" do |
| 98 | + begin |
| 99 | + file = Tempfile.new('injectable_env_test') |
| 100 | + file.write(%{var injected="#{UnpaddedPlaceholder}"}) |
| 101 | + file.rewind |
| 102 | + |
| 103 | + InjectableEnv.replace(file.path) |
| 104 | + |
| 105 | + expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}' |
| 106 | + actual_value=file.read |
| 107 | + expect(actual_value.index(expected_value)).to eq(0) |
| 108 | + # Closing double-quote is padded out but still last char. |
| 109 | + actual_size = actual_value.size |
| 110 | + expect(actual_value.index(/\"\Z/)).to eq(actual_size-1) |
| 111 | + ensure |
| 112 | + if file |
| 113 | + file.close |
| 114 | + file.unlink |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + it "preserves character length of bundle" do |
| 120 | + begin |
| 121 | + placeholder_size = Placeholder.size |
| 122 | + file = Tempfile.new('injectable_env_test') |
| 123 | + file.write(Placeholder) |
| 124 | + file.rewind |
| 125 | + |
| 126 | + InjectableEnv.replace(file.path) |
| 127 | + |
| 128 | + expected_value = '{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}' |
| 129 | + actual_value = file.read |
| 130 | + replaced_size = actual_value.size |
| 131 | + expect(replaced_size).to eq(placeholder_size) |
| 132 | + expect(actual_value.index(expected_value)).to eq(0) |
83 | 133 | ensure
|
84 | 134 | if file
|
85 | 135 | file.close
|
|
0 commit comments