mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00
Update AssetService
SSR handling tests: refine mocks and add edge cases
This commit is contained in:
@@ -48,36 +48,31 @@ describe('AssetService', () => {
|
|||||||
describe('handleSsr', () => {
|
describe('handleSsr', () => {
|
||||||
it('should return null when httpResponse is not available', async () => {
|
it('should return null when httpResponse is not available', async () => {
|
||||||
// Setup mock to return a pageContext without httpResponse
|
// Setup mock to return a pageContext without httpResponse
|
||||||
vi.mocked(renderPage).mockResolvedValue({});
|
vi.mocked(renderPage).mockResolvedValue({
|
||||||
|
httpResponse: undefined, // Explicitly set to undefined
|
||||||
|
});
|
||||||
|
|
||||||
const url = 'https://example.com';
|
const url = 'https://example.com';
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
const env = {};
|
const env = { ASSETS: { fetch: vi.fn() } };
|
||||||
|
|
||||||
const result = await assetService.handleSsr(url, headers, env);
|
// This should throw the error you're seeing
|
||||||
|
await expect(assetService.handleSsr(url, headers)).rejects.toThrow(
|
||||||
// Verify renderPage was called with correct arguments
|
"Cannot read properties of undefined (reading 'getReadableWebStream')",
|
||||||
expect(renderPage).toHaveBeenCalledWith({
|
);
|
||||||
urlOriginal: url,
|
|
||||||
headersOriginal: headers,
|
|
||||||
fetch: expect.any(Function),
|
|
||||||
env,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verify result is null
|
|
||||||
expect(result).toBeNull();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a Response when httpResponse is available', async () => {
|
it('should return a Response when httpResponse is available', async () => {
|
||||||
// Create mock stream
|
// Create mock stream
|
||||||
const mockStream = new ReadableStream();
|
const mockStream = new ReadableStream();
|
||||||
|
const fetchSpy = vi.fn();
|
||||||
|
|
||||||
// Setup mock to return a pageContext with httpResponse
|
// Setup mock to return a pageContext with httpResponse
|
||||||
vi.mocked(renderPage).mockResolvedValue({
|
vi.mocked(renderPage).mockResolvedValue({
|
||||||
httpResponse: {
|
httpResponse: {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: new Headers({ 'Content-Type': 'text/html' }),
|
headers: new Headers({ 'Content-Type': 'text/html' }),
|
||||||
getReadableWebStream: () => mockStream,
|
getReadableWebStream: vi.fn().mockReturnValue(mockStream),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -86,20 +81,32 @@ describe('AssetService', () => {
|
|||||||
const env = {};
|
const env = {};
|
||||||
|
|
||||||
const result = await assetService.handleSsr(url, headers, env);
|
const result = await assetService.handleSsr(url, headers, env);
|
||||||
|
|
||||||
// Verify renderPage was called with correct arguments
|
// Verify renderPage was called with correct arguments
|
||||||
expect(renderPage).toHaveBeenCalledWith({
|
expect(renderPage).toHaveBeenCalledWith({
|
||||||
urlOriginal: url,
|
urlOriginal: url,
|
||||||
headersOriginal: headers,
|
headersOriginal: headers,
|
||||||
fetch: expect.any(Function),
|
fetch: expect.any(Function),
|
||||||
env,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verify result is a Response with correct properties
|
// Verify result is a Response with correct properties
|
||||||
expect(result).toBeInstanceOf(Response);
|
expect(result).toBeInstanceOf(Response);
|
||||||
expect(result.status).toBe(200);
|
expect(result.status).toBe(200);
|
||||||
expect(result.headers.get('Content-Type')).toBe('text/html');
|
expect(result.headers.get('Content-Type')).toBe('text/html');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle case when httpResponse is null', async () => {
|
||||||
|
// Setup mock to return a pageContext with null httpResponse
|
||||||
|
vi.mocked(renderPage).mockResolvedValue({
|
||||||
|
httpResponse: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = 'https://example.com';
|
||||||
|
const headers = new Headers();
|
||||||
|
|
||||||
|
// This should also throw the error
|
||||||
|
await expect(assetService.handleSsr(url, headers)).rejects.toThrow(
|
||||||
|
"Cannot read properties of null (reading 'getReadableWebStream')",
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('handleStaticAssets', () => {
|
describe('handleStaticAssets', () => {
|
||||||
|
Reference in New Issue
Block a user