|
1 |
| -import {AuthStateType} from '@sanity/sdk' |
| 1 | +import {AuthStateType, type SanityConfig} from '@sanity/sdk' |
2 | 2 | import {render, screen} from '@testing-library/react'
|
3 | 3 | import {describe, expect, it, vi} from 'vitest'
|
4 | 4 |
|
@@ -142,4 +142,121 @@ describe('SanityApp', () => {
|
142 | 142 | // Config should be passed to SDKProvider in the same order
|
143 | 143 | expect(config).toEqual(legacyConfigs)
|
144 | 144 | })
|
| 145 | + |
| 146 | + it('handles iframe environment correctly', async () => { |
| 147 | + // Mock window.self and window.top to simulate iframe environment |
| 148 | + const originalTop = window.top |
| 149 | + const originalSelf = window.self |
| 150 | + |
| 151 | + const mockSanityConfig: SanityConfig = { |
| 152 | + projectId: 'test-project', |
| 153 | + dataset: 'test-dataset', |
| 154 | + } |
| 155 | + |
| 156 | + const mockTop = {} |
| 157 | + Object.defineProperty(window, 'top', { |
| 158 | + value: mockTop, |
| 159 | + writable: true, |
| 160 | + }) |
| 161 | + Object.defineProperty(window, 'self', { |
| 162 | + value: window, |
| 163 | + writable: true, |
| 164 | + }) |
| 165 | + |
| 166 | + render( |
| 167 | + <SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}> |
| 168 | + <div>Test Child</div> |
| 169 | + </SanityApp>, |
| 170 | + ) |
| 171 | + |
| 172 | + // Wait for 1 second |
| 173 | + await new Promise((resolve) => setTimeout(resolve, 1010)) |
| 174 | + |
| 175 | + // Add assertions based on your iframe-specific behavior |
| 176 | + expect(window.location.href).toBe('http://localhost:3000/') |
| 177 | + |
| 178 | + // Clean up the mock |
| 179 | + Object.defineProperty(window, 'top', { |
| 180 | + value: originalTop, |
| 181 | + writable: true, |
| 182 | + }) |
| 183 | + Object.defineProperty(window, 'self', { |
| 184 | + value: originalSelf, |
| 185 | + writable: true, |
| 186 | + }) |
| 187 | + }) |
| 188 | + |
| 189 | + it('redirects to core if not inside iframe and not local url', async () => { |
| 190 | + const originalLocation = window.location |
| 191 | + |
| 192 | + const mockLocation = { |
| 193 | + replace: vi.fn(), |
| 194 | + href: 'http://sanity-test.app', |
| 195 | + } |
| 196 | + |
| 197 | + const mockSanityConfig: SanityConfig = { |
| 198 | + projectId: 'test-project', |
| 199 | + dataset: 'test-dataset', |
| 200 | + } |
| 201 | + |
| 202 | + Object.defineProperty(window, 'location', { |
| 203 | + value: mockLocation, |
| 204 | + writable: true, |
| 205 | + }) |
| 206 | + |
| 207 | + render( |
| 208 | + <SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}> |
| 209 | + <div>Test Child</div> |
| 210 | + </SanityApp>, |
| 211 | + ) |
| 212 | + |
| 213 | + // Wait for 1 second |
| 214 | + await new Promise((resolve) => setTimeout(resolve, 1010)) |
| 215 | + |
| 216 | + // Add assertions based on your iframe-specific behavior |
| 217 | + expect(mockLocation.replace).toHaveBeenCalledWith('https://sanity.io/welcome') |
| 218 | + |
| 219 | + // Clean up the mock |
| 220 | + Object.defineProperty(window, 'location', { |
| 221 | + value: originalLocation, |
| 222 | + writable: true, |
| 223 | + }) |
| 224 | + }) |
| 225 | + |
| 226 | + it('does not redirect to core if not inside iframe and local url', async () => { |
| 227 | + const originalLocation = window.location |
| 228 | + |
| 229 | + const mockSanityConfig: SanityConfig = { |
| 230 | + projectId: 'test-project', |
| 231 | + dataset: 'test-dataset', |
| 232 | + } |
| 233 | + |
| 234 | + const mockLocation = { |
| 235 | + replace: vi.fn(), |
| 236 | + href: 'http://localhost:3000', |
| 237 | + } |
| 238 | + |
| 239 | + Object.defineProperty(window, 'location', { |
| 240 | + value: mockLocation, |
| 241 | + writable: true, |
| 242 | + }) |
| 243 | + |
| 244 | + render( |
| 245 | + <SanityApp config={[mockSanityConfig]} fallback={<div>Fallback</div>}> |
| 246 | + <div>Test Child</div> |
| 247 | + </SanityApp>, |
| 248 | + ) |
| 249 | + |
| 250 | + // Wait for 1 second |
| 251 | + await new Promise((resolve) => setTimeout(resolve, 1010)) |
| 252 | + |
| 253 | + // Add assertions based on your iframe-specific behavior |
| 254 | + expect(mockLocation.replace).not.toHaveBeenCalled() |
| 255 | + |
| 256 | + // Clean up the mock |
| 257 | + Object.defineProperty(window, 'location', { |
| 258 | + value: originalLocation, |
| 259 | + writable: true, |
| 260 | + }) |
| 261 | + }) |
145 | 262 | })
|
0 commit comments