|
| 1 | +import React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { useTabTheme } from '@mongodb-js/compass-components/src/components/workspace-tabs/use-tab-theme'; |
| 4 | +import { render } from '@mongodb-js/testing-library-compass'; |
| 5 | +import { ConnectionThemeProvider } from './connection-tab-theme-provider'; |
| 6 | + |
| 7 | +const CONNECTION_INFO = { |
| 8 | + id: '1234', |
| 9 | + connectionOptions: { |
| 10 | + connectionString: 'mongodb://localhost:27017', |
| 11 | + }, |
| 12 | + favorite: { |
| 13 | + color: 'color3', |
| 14 | + name: 'my kingdom for a hook', |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +const CONNECTION_INFO_NO_COLOR = { |
| 19 | + id: '1234', |
| 20 | + connectionOptions: { |
| 21 | + connectionString: 'mongodb://localhost:27017', |
| 22 | + }, |
| 23 | + favorite: { |
| 24 | + name: 'look what is done cannot be now amended', |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +describe('ConnectionThemeProvider', function () { |
| 29 | + describe('when a connection does not exist', function () { |
| 30 | + it('should not provide a theme to useTabTheme', function () { |
| 31 | + let capturedTheme: ReturnType<typeof useTabTheme> = undefined; |
| 32 | + |
| 33 | + const TestComponent = () => { |
| 34 | + capturedTheme = useTabTheme(); |
| 35 | + return null; |
| 36 | + }; |
| 37 | + |
| 38 | + render( |
| 39 | + <ConnectionThemeProvider connectionId="nonexistent-connection"> |
| 40 | + <TestComponent /> |
| 41 | + </ConnectionThemeProvider>, |
| 42 | + { |
| 43 | + connections: [CONNECTION_INFO], |
| 44 | + } |
| 45 | + ); |
| 46 | + |
| 47 | + expect(capturedTheme).to.equal(undefined); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('when a connection exists with a valid color', function () { |
| 52 | + it('should provide the correct theme to useTabTheme', function () { |
| 53 | + let capturedTheme: ReturnType<typeof useTabTheme> = undefined; |
| 54 | + |
| 55 | + const TestComponent = () => { |
| 56 | + capturedTheme = useTabTheme(); |
| 57 | + return null; |
| 58 | + }; |
| 59 | + |
| 60 | + render( |
| 61 | + <ConnectionThemeProvider connectionId={CONNECTION_INFO.id}> |
| 62 | + <TestComponent /> |
| 63 | + </ConnectionThemeProvider>, |
| 64 | + { |
| 65 | + connections: [CONNECTION_INFO], |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + expect(capturedTheme).to.have.property( |
| 70 | + '--workspace-tab-background-color', |
| 71 | + '#D5EFFF' |
| 72 | + ); |
| 73 | + expect(capturedTheme).to.have.property( |
| 74 | + '--workspace-tab-top-border-color', |
| 75 | + '#D5EFFF' |
| 76 | + ); |
| 77 | + expect(capturedTheme).to.have.property( |
| 78 | + '--workspace-tab-selected-top-border-color', |
| 79 | + '#C2E5FF' |
| 80 | + ); |
| 81 | + expect(capturedTheme).to.have.deep.property('&:focus-visible'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('when a connection exists without a color', function () { |
| 86 | + it('should not provide a theme to useTabTheme', function () { |
| 87 | + let capturedTheme: ReturnType<typeof useTabTheme> = undefined; |
| 88 | + |
| 89 | + const TestComponent = () => { |
| 90 | + capturedTheme = useTabTheme(); |
| 91 | + return null; |
| 92 | + }; |
| 93 | + |
| 94 | + render( |
| 95 | + <ConnectionThemeProvider connectionId={CONNECTION_INFO_NO_COLOR.id}> |
| 96 | + <TestComponent /> |
| 97 | + </ConnectionThemeProvider>, |
| 98 | + { |
| 99 | + connections: [CONNECTION_INFO_NO_COLOR], |
| 100 | + } |
| 101 | + ); |
| 102 | + |
| 103 | + expect(capturedTheme).to.equal(undefined); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('when a connection exists with an invalid color', function () { |
| 108 | + it('should not provide a theme to useTabTheme', function () { |
| 109 | + let capturedTheme: ReturnType<typeof useTabTheme> = undefined; |
| 110 | + const INVALID_COLOR_CONNECTION = { |
| 111 | + id: '5678', |
| 112 | + connectionOptions: { |
| 113 | + connectionString: 'mongodb://localhost:27017', |
| 114 | + }, |
| 115 | + favorite: { |
| 116 | + color: 'notavalidcolor', |
| 117 | + name: 'invalid color connection', |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + const TestComponent = () => { |
| 122 | + capturedTheme = useTabTheme(); |
| 123 | + return null; |
| 124 | + }; |
| 125 | + |
| 126 | + render( |
| 127 | + <ConnectionThemeProvider connectionId={INVALID_COLOR_CONNECTION.id}> |
| 128 | + <TestComponent /> |
| 129 | + </ConnectionThemeProvider>, |
| 130 | + { |
| 131 | + connections: [INVALID_COLOR_CONNECTION], |
| 132 | + } |
| 133 | + ); |
| 134 | + |
| 135 | + expect(capturedTheme).to.equal(undefined); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('when a connection color is updated', function () { |
| 140 | + it('should update the theme provided to useTabTheme', async function () { |
| 141 | + let capturedTheme: ReturnType<typeof useTabTheme> = undefined; |
| 142 | + const connection = { |
| 143 | + id: 'changeable-color', |
| 144 | + connectionOptions: { |
| 145 | + connectionString: 'mongodb://localhost:27017', |
| 146 | + }, |
| 147 | + favorite: { |
| 148 | + color: 'color3', // Initial color |
| 149 | + name: 'changing colors', |
| 150 | + }, |
| 151 | + }; |
| 152 | + |
| 153 | + const TestComponent = () => { |
| 154 | + capturedTheme = useTabTheme(); |
| 155 | + return <div>Theme consumer</div>; |
| 156 | + }; |
| 157 | + |
| 158 | + const { rerender, connectionsStore } = render( |
| 159 | + <ConnectionThemeProvider connectionId={connection.id}> |
| 160 | + <TestComponent /> |
| 161 | + </ConnectionThemeProvider>, |
| 162 | + { |
| 163 | + connections: [connection], |
| 164 | + } |
| 165 | + ); |
| 166 | + |
| 167 | + // Initial theme should have color3 values |
| 168 | + expect(capturedTheme).to.not.equal(null); |
| 169 | + expect(capturedTheme).to.have.property( |
| 170 | + '--workspace-tab-background-color', |
| 171 | + '#D5EFFF' |
| 172 | + ); |
| 173 | + |
| 174 | + // Update the connection color |
| 175 | + await connectionsStore.actions.saveEditedConnection({ |
| 176 | + ...connection, |
| 177 | + favorite: { |
| 178 | + ...connection.favorite, |
| 179 | + color: 'color1', // Change to color1 |
| 180 | + }, |
| 181 | + }); |
| 182 | + |
| 183 | + // Re-render to pick up the new color |
| 184 | + rerender( |
| 185 | + <ConnectionThemeProvider connectionId={connection.id}> |
| 186 | + <TestComponent /> |
| 187 | + </ConnectionThemeProvider> |
| 188 | + ); |
| 189 | + |
| 190 | + // Theme should have been updated with color1 values |
| 191 | + expect(capturedTheme).to.not.equal(null); |
| 192 | + // color1 should have a different background color than color3 |
| 193 | + expect(capturedTheme) |
| 194 | + .to.have.property('--workspace-tab-background-color') |
| 195 | + .that.does.not.equal('#D5EFFF'); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments