-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(standard): add theme color meta (#772)
* feat(standard): add theme color meta * perf: reduce code by merging empty array case into if clause * perf: reduce code by updating keyval meta def factory * docs: add theme color to comparison page
- Loading branch information
Showing
11 changed files
with
226 additions
and
3 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
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
100 changes: 100 additions & 0 deletions
100
projects/ngx-meta/src/standard/src/standard-theme-color-metadata-provider.spec.ts
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,100 @@ | ||
import { MetadataSetter, NgxMetaMetaService } from '../../core' | ||
import { Standard } from './standard' | ||
import { TestBed } from '@angular/core/testing' | ||
import { MockProvider } from 'ng-mocks' | ||
import { | ||
__STANDARD_THEME_COLOR_METADATA_SETTER_FACTORY, | ||
_STANDARD_THEME_COLOR_KEY, | ||
} from './standard-theme-color-metadata-provider' | ||
import { enableAutoSpy } from '@/ngx-meta/test/enable-auto-spy' | ||
import { MetaDefinition } from '@angular/platform-browser' | ||
import { StandardThemeColorMetadataObject } from './standard-theme-color-metadata' | ||
|
||
describe('Standard theme color metadata', () => { | ||
enableAutoSpy() | ||
let sut: MetadataSetter<Standard[typeof _STANDARD_THEME_COLOR_KEY]> | ||
let metaService: jasmine.SpyObj<NgxMetaMetaService> | ||
|
||
const DUMMY_COLOR = 'black' | ||
const DUMMY_MEDIA = '(prefers-color-scheme: dark)' | ||
|
||
beforeEach(() => { | ||
sut = makeSut() | ||
metaService = TestBed.inject( | ||
NgxMetaMetaService, | ||
) as jasmine.SpyObj<NgxMetaMetaService> | ||
}) | ||
|
||
it('should call the meta service with no value when no value provided', () => { | ||
sut(undefined) | ||
|
||
expect(metaService.set).toHaveBeenCalledOnceWith( | ||
jasmine.anything(), | ||
undefined, | ||
) | ||
}) | ||
|
||
it('should call the meta service with the specified content when a string value is provided', () => { | ||
sut(DUMMY_COLOR) | ||
|
||
expect(metaService.set).toHaveBeenCalledOnceWith( | ||
jasmine.anything(), | ||
DUMMY_COLOR, | ||
) | ||
}) | ||
|
||
it('should call the meta service with no value when an empty array is provided', () => { | ||
sut([]) | ||
|
||
expect(metaService.set).toHaveBeenCalledOnceWith( | ||
jasmine.anything(), | ||
undefined, | ||
) | ||
}) | ||
|
||
it('should call the meta service with the specified content and media when object values are provided', () => { | ||
const firstMediaDefinition = { | ||
color: DUMMY_COLOR, | ||
media: DUMMY_MEDIA, | ||
} satisfies MetaDefinition & StandardThemeColorMetadataObject | ||
const secondMediaDefinition = { | ||
color: 'white', | ||
} satisfies MetaDefinition & StandardThemeColorMetadataObject | ||
sut([firstMediaDefinition, secondMediaDefinition]) | ||
|
||
expect(metaService.set).toHaveBeenCalledWith( | ||
jasmine.anything(), | ||
firstMediaDefinition.color, | ||
) | ||
expect(metaService.set).toHaveBeenCalledWith( | ||
jasmine.anything(), | ||
secondMediaDefinition.color, | ||
) | ||
expect( | ||
metaService.set.calls | ||
.argsFor(0)[0] | ||
.withContent(firstMediaDefinition.color), | ||
).toEqual({ | ||
name: 'theme-color', | ||
content: firstMediaDefinition.color, | ||
media: firstMediaDefinition.media, | ||
}) | ||
expect( | ||
metaService.set.calls | ||
.argsFor(1)[0] | ||
.withContent(secondMediaDefinition.color), | ||
).toEqual({ | ||
name: 'theme-color', | ||
content: secondMediaDefinition.color, | ||
}) | ||
}) | ||
}) | ||
|
||
function makeSut(): MetadataSetter<Standard[typeof _STANDARD_THEME_COLOR_KEY]> { | ||
TestBed.configureTestingModule({ | ||
providers: [MockProvider(NgxMetaMetaService)], | ||
}) | ||
return __STANDARD_THEME_COLOR_METADATA_SETTER_FACTORY( | ||
TestBed.inject(NgxMetaMetaService), | ||
) | ||
} |
52 changes: 52 additions & 0 deletions
52
projects/ngx-meta/src/standard/src/standard-theme-color-metadata-provider.ts
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,52 @@ | ||
import { makeStandardMetadataProvider } from './make-standard-metadata-provider' | ||
import { | ||
MetadataSetterFactory, | ||
NgxMetaMetaService, | ||
} from '@davidlj95/ngx-meta/core' | ||
import { Standard } from './standard' | ||
import { makeStandardMetaDefinition } from './make-standard-meta-definition' | ||
import { StandardThemeColorMetadataObject } from './standard-theme-color-metadata' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const _STANDARD_THEME_COLOR_KEY = 'themeColor' satisfies keyof Standard | ||
|
||
const META_NAME = 'theme-color' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const __STANDARD_THEME_COLOR_METADATA_SETTER_FACTORY: MetadataSetterFactory< | ||
Standard[typeof _STANDARD_THEME_COLOR_KEY] | ||
> = (ngxMetaMetaService: NgxMetaMetaService) => (value) => { | ||
const isValueAnArray = isStandardThemeColorArray(value) | ||
const baseMetaDefinition = makeStandardMetaDefinition(META_NAME) | ||
if (!value || !isValueAnArray || !value.length) { | ||
ngxMetaMetaService.set( | ||
baseMetaDefinition, | ||
isValueAnArray ? undefined : value, | ||
) | ||
return | ||
} | ||
for (const { media, color } of value) { | ||
ngxMetaMetaService.set( | ||
makeStandardMetaDefinition(META_NAME, media ? { extras: { media } } : {}), | ||
color, | ||
) | ||
} | ||
} | ||
|
||
const isStandardThemeColorArray = ( | ||
value: Standard['themeColor'], | ||
): value is ReadonlyArray<StandardThemeColorMetadataObject> => | ||
Array.isArray(value) | ||
|
||
/** | ||
* Manages the {@link Standard.themeColor} metadata | ||
* @public | ||
*/ | ||
export const STANDARD_THEME_COLOR_METADATA_PROVIDER = | ||
makeStandardMetadataProvider(_STANDARD_THEME_COLOR_KEY, { | ||
s: __STANDARD_THEME_COLOR_METADATA_SETTER_FACTORY, | ||
}) |
22 changes: 22 additions & 0 deletions
22
projects/ngx-meta/src/standard/src/standard-theme-color-metadata.ts
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,22 @@ | ||
/** | ||
* See {@link Standard.themeColor} | ||
* @public | ||
*/ | ||
export type StandardThemeColorMetadata = | ||
| string | ||
| ReadonlyArray<StandardThemeColorMetadataObject> | ||
|
||
/** | ||
* See {@link Standard.themeColor} | ||
* @public | ||
*/ | ||
export interface StandardThemeColorMetadataObject { | ||
/** | ||
* See {@link Standard.themeColor} | ||
*/ | ||
color: string | ||
/** | ||
* See {@link Standard.themeColor} | ||
*/ | ||
media?: string | ||
} |
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