|
| 1 | +import test from 'ava'; |
| 2 | +import delay from 'delay'; |
| 3 | +import stripAnsi from 'strip-ansi'; |
| 4 | +import React from 'react'; |
| 5 | +import {render, Box, Text} from '../src/index.js'; |
| 6 | +import createStdout from './helpers/create-stdout.js'; |
| 7 | + |
| 8 | +test.serial('clear screen when terminal width decreases', async t => { |
| 9 | + const stdout = createStdout(100); |
| 10 | + |
| 11 | + function Test() { |
| 12 | + return ( |
| 13 | + <Box borderStyle="round"> |
| 14 | + <Text>Hello World</Text> |
| 15 | + </Box> |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + render(<Test />, {stdout}); |
| 20 | + |
| 21 | + const initialOutput = stripAnsi( |
| 22 | + (stdout.write as any).firstCall.args[0] as string, |
| 23 | + ); |
| 24 | + t.true(initialOutput.includes('Hello World')); |
| 25 | + t.true(initialOutput.includes('╭')); // Box border |
| 26 | + |
| 27 | + // Decrease width - should trigger clear and rerender |
| 28 | + stdout.columns = 50; |
| 29 | + stdout.emit('resize'); |
| 30 | + await delay(100); |
| 31 | + |
| 32 | + // Verify the output was updated for smaller width |
| 33 | + const lastOutput = stripAnsi( |
| 34 | + (stdout.write as any).lastCall.args[0] as string, |
| 35 | + ); |
| 36 | + t.true(lastOutput.includes('Hello World')); |
| 37 | + t.true(lastOutput.includes('╭')); // Box border |
| 38 | + t.not(initialOutput, lastOutput); // Output should change due to width |
| 39 | +}); |
| 40 | + |
| 41 | +test.serial('no screen clear when terminal width increases', async t => { |
| 42 | + const stdout = createStdout(50); |
| 43 | + |
| 44 | + function Test() { |
| 45 | + return ( |
| 46 | + <Box borderStyle="round"> |
| 47 | + <Text>Test</Text> |
| 48 | + </Box> |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + render(<Test />, {stdout}); |
| 53 | + |
| 54 | + const initialOutput = (stdout.write as any).firstCall.args[0] as string; |
| 55 | + |
| 56 | + // Increase width - should rerender but not clear |
| 57 | + stdout.columns = 100; |
| 58 | + stdout.emit('resize'); |
| 59 | + await delay(100); |
| 60 | + |
| 61 | + const lastOutput = (stdout.write as any).lastCall.args[0] as string; |
| 62 | + |
| 63 | + // When increasing width, we don't clear, so we should see eraseLines used for incremental update |
| 64 | + // But when decreasing, the clear() is called which also uses eraseLines |
| 65 | + // The key difference: decreasing width triggers an explicit clear before render |
| 66 | + t.not(stripAnsi(initialOutput), stripAnsi(lastOutput)); |
| 67 | + t.true(stripAnsi(lastOutput).includes('Test')); |
| 68 | +}); |
| 69 | + |
| 70 | +test.serial( |
| 71 | + 'consecutive width decreases trigger screen clear each time', |
| 72 | + async t => { |
| 73 | + const stdout = createStdout(100); |
| 74 | + |
| 75 | + function Test() { |
| 76 | + return ( |
| 77 | + <Box borderStyle="round"> |
| 78 | + <Text>Content</Text> |
| 79 | + </Box> |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + render(<Test />, {stdout}); |
| 84 | + |
| 85 | + const initialOutput = stripAnsi( |
| 86 | + (stdout.write as any).firstCall.args[0] as string, |
| 87 | + ); |
| 88 | + |
| 89 | + // First decrease |
| 90 | + stdout.columns = 80; |
| 91 | + stdout.emit('resize'); |
| 92 | + await delay(100); |
| 93 | + |
| 94 | + const afterFirstDecrease = stripAnsi( |
| 95 | + (stdout.write as any).lastCall.args[0] as string, |
| 96 | + ); |
| 97 | + t.not(initialOutput, afterFirstDecrease); |
| 98 | + t.true(afterFirstDecrease.includes('Content')); |
| 99 | + |
| 100 | + // Second decrease |
| 101 | + stdout.columns = 60; |
| 102 | + stdout.emit('resize'); |
| 103 | + await delay(100); |
| 104 | + |
| 105 | + const afterSecondDecrease = stripAnsi( |
| 106 | + (stdout.write as any).lastCall.args[0] as string, |
| 107 | + ); |
| 108 | + t.not(afterFirstDecrease, afterSecondDecrease); |
| 109 | + t.true(afterSecondDecrease.includes('Content')); |
| 110 | + }, |
| 111 | +); |
| 112 | + |
| 113 | +test.serial('width decrease clears lastOutput to force rerender', async t => { |
| 114 | + const stdout = createStdout(100); |
| 115 | + |
| 116 | + function Test() { |
| 117 | + return ( |
| 118 | + <Box borderStyle="round"> |
| 119 | + <Text>Test Content</Text> |
| 120 | + </Box> |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + const {rerender} = render(<Test />, {stdout}); |
| 125 | + |
| 126 | + const initialOutput = stripAnsi( |
| 127 | + (stdout.write as any).firstCall.args[0] as string, |
| 128 | + ); |
| 129 | + |
| 130 | + // Decrease width - with a border, this will definitely change the output |
| 131 | + stdout.columns = 50; |
| 132 | + stdout.emit('resize'); |
| 133 | + await delay(100); |
| 134 | + |
| 135 | + const afterResizeOutput = stripAnsi( |
| 136 | + (stdout.write as any).lastCall.args[0] as string, |
| 137 | + ); |
| 138 | + |
| 139 | + // Outputs should be different because the border width changed |
| 140 | + t.not(initialOutput, afterResizeOutput); |
| 141 | + t.true(afterResizeOutput.includes('Test Content')); |
| 142 | + |
| 143 | + // Now try to rerender with a different component |
| 144 | + rerender( |
| 145 | + <Box borderStyle="round"> |
| 146 | + <Text>Updated Content</Text> |
| 147 | + </Box>, |
| 148 | + ); |
| 149 | + await delay(100); |
| 150 | + |
| 151 | + // Verify content was updated |
| 152 | + t.true( |
| 153 | + stripAnsi((stdout.write as any).lastCall.args[0] as string).includes( |
| 154 | + 'Updated Content', |
| 155 | + ), |
| 156 | + ); |
| 157 | +}); |
0 commit comments