-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Set head data of Experiment page based on name, description an…
…d its theme's image (#1285) * refactor: Regard Block page as having a "Block" type in its structured data * feat: Add `useHeadDataFromExperiment` hook to set head data in Experiment component * test: Add tests for `useHeadDataFromExperiment` hook * type: Add `Experiment` type to `useExperiment hook` * type: Refactor useHeadDataFromExperiment to accept null as experiment parameter
- Loading branch information
1 parent
4ab20d4
commit 4061f91
Showing
6 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { vi, describe, beforeEach, test, expect } from 'vitest'; | ||
import useHeadDataFromExperiment from './useHeadDataFromExperiment'; | ||
import IExperiment from '@/types/Experiment'; | ||
|
||
describe('useHeadDataFromExperiment', () => { | ||
const mockSetHeadData = vi.fn(); | ||
const mockResetHeadData = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
// Mock window.location | ||
Object.defineProperty(window, 'location', { | ||
value: { href: 'https://example.com/experiment' }, | ||
writable: true | ||
}); | ||
}); | ||
|
||
test('should set head data when experiment is provided', () => { | ||
const mockExperiment: IExperiment = { | ||
name: 'Test Experiment', | ||
description: '<p>This is a <strong>test</strong> description</p>', | ||
theme: { | ||
logo: { | ||
file: '/test-logo.jpg' | ||
} | ||
} | ||
} as IExperiment; | ||
|
||
renderHook(() => useHeadDataFromExperiment(mockExperiment, mockSetHeadData, mockResetHeadData)); | ||
|
||
expect(mockSetHeadData).toHaveBeenCalledWith({ | ||
title: 'Test Experiment', | ||
description: 'This is a test description', | ||
image: '/test-logo.jpg', | ||
url: 'https://example.com/experiment', | ||
structuredData: { | ||
"@type": "Experiment", | ||
}, | ||
}); | ||
}); | ||
|
||
test('should use default image when theme logo is not provided', () => { | ||
const mockExperiment: IExperiment = { | ||
name: 'Test Experiment', | ||
description: 'Test description', | ||
} as IExperiment; | ||
|
||
renderHook(() => useHeadDataFromExperiment(mockExperiment, mockSetHeadData, mockResetHeadData)); | ||
|
||
expect(mockSetHeadData).toHaveBeenCalledWith(expect.objectContaining({ | ||
image: "/images/aml-logolarge-1200x630.jpg", | ||
})); | ||
}); | ||
|
||
test('should not set head data when experiment is null', () => { | ||
renderHook(() => useHeadDataFromExperiment(null, mockSetHeadData, mockResetHeadData)); | ||
|
||
expect(mockSetHeadData).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should reset head data on unmount', () => { | ||
const { unmount } = renderHook(() => useHeadDataFromExperiment({} as IExperiment, mockSetHeadData, mockResetHeadData)); | ||
|
||
unmount(); | ||
|
||
expect(mockResetHeadData).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect } from 'react'; | ||
import IExperiment from "@/types/Experiment"; | ||
import { DocumentHeadSlice } from '@/util/stores'; | ||
|
||
const useHeadDataFromExperiment = (experiment: IExperiment | null, setHeadData: DocumentHeadSlice['setHeadData'], resetHeadData: DocumentHeadSlice['resetHeadData']) => { | ||
useEffect(() => { | ||
if (experiment) { | ||
// De-htmlify the description's HTML string | ||
const descriptionDiv = document.createElement("div"); | ||
descriptionDiv.innerHTML = experiment.description; | ||
const description = descriptionDiv.textContent || ''; | ||
|
||
setHeadData({ | ||
title: experiment.name, | ||
description: description, | ||
image: experiment.theme?.logo?.file ?? "/images/aml-logolarge-1200x630.jpg", | ||
url: window.location.href, | ||
structuredData: { | ||
"@type": "Experiment", | ||
}, | ||
}); | ||
} | ||
|
||
return () => { | ||
resetHeadData(); | ||
}; | ||
}, [experiment, setHeadData, resetHeadData]); | ||
}; | ||
|
||
export default useHeadDataFromExperiment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters