Skip to content

Commit

Permalink
fix: export and test guessStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Nov 27, 2023
1 parent 5a00f07 commit df24dd4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
55 changes: 40 additions & 15 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import { Colorful } from './index.js';
/* eslint-disable @typescript-eslint/naming-convention */
import * as builderClasses from './index.js';
import StyleBuilder from './lib/style_builder.js';

describe('Style Builders', () => {
const styleNames = [
'Colorful',
'Graybeard',
'Neutrino',
const styles = [
{ name: 'Colorful', builderClass: builderClasses.Colorful },
{ name: 'Graybeard', builderClass: builderClasses.Graybeard },
{ name: 'Neutrino', builderClass: builderClasses.Neutrino },
];

it(`should have the correct ${styleNames.length} styles`, () => {
const keys1 = Array.from(Object.keys(builderClasses)).sort();
const keys2 = styleNames.sort();
expect(keys1).toEqual(keys2);
});

Object.entries(builderClasses).forEach(([styleName, builderClass]) => {
it(`should create and test an instance of ${styleName}`, () => {
styles.forEach(({ name, builderClass }) => {
it(`should create and test an instance of ${name}`, () => {
const builder = new builderClass();
expect(builder).toBeInstanceOf(StyleBuilder);
expect(typeof builder.name).toBe('string');
expect(builder.name).toBe(name);

builder.baseUrl = 'https://example.org';
const style = builder.build();
expect(JSON.stringify(style).length).toBeGreaterThan(50000);

expect(style.name).toBe('versatiles-' + styleName.toLowerCase());
expect(style.name).toBe('versatiles-' + name.toLowerCase());
expect(style.glyphs).toBe('https://example.org/assets/fonts/{fontstack}/{range}.pbf');
expect(style.sprite).toBe('https://example.org/assets/sprites/sprites');
expect(Object.keys(style.sources).join(',')).toBe('versatiles-shortbread');
Expand All @@ -36,9 +31,39 @@ describe('Style Builders', () => {
});

describe('Colorful', () => {
const colorful = new Colorful();
const colorful = new builderClasses.Colorful();
colorful.baseUrl = 'https://dev.null';
colorful.defaultColors.commercial = '#f00';
const style = colorful.build();
expect(style.glyphs).toBe('https://dev.null/assets/fonts/{fontstack}/{range}.pbf');
});

describe('guessStyle', () => {
it('should build raster styles', () => {
const style = builderClasses.guessStyle({
type: 'raster',
tiles: [],
format: 'avif',
});
expect(style).toStrictEqual({
layers: [{ id: 'raster', source: 'rasterSource', type: 'raster' }],
sources: { rasterSource: { format: 'avif', tilejson: '3.0.0', tiles: [], type: 'raster' } },
version: 8,
});
});


it('should build vector styles', () => {
const style = builderClasses.guessStyle({
type: 'vector',
tiles: [],
format: 'pbf',
vector_layers: [],
});
expect(style).toStrictEqual({
layers: [{ id: 'background', paint: { 'background-color': '#fff' }, type: 'background' }],
sources: { vectorSource: { format: 'pbf', tilejson: '3.0.0', tiles: [], type: 'vector', 'vector_layers': [] } },
'version': 8,
});
});
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

export type { MaplibreStyle } from './lib/types.js';

export { default as guessStyle } from './lib/style_guesser.js';

export { default as Colorful } from './style/colorful.js';
export { default as Graybeard } from './style/graybeard.js';
export { default as Neutrino } from './style/neutrino.js';
2 changes: 1 addition & 1 deletion src/lib/style_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default abstract class StyleBuilder<Subclass extends StyleBuilder<Subclas
if (this.hideLabels) layers = layers.filter(l => l.type !== 'symbol');

style.layers = layers;
style.name = 'versatiles-' + this.name;
style.name = 'versatiles-' + this.name.toLowerCase();
style.glyphs = resolveUrl(this.baseUrl, this.glyphsUrl);
style.sprite = resolveUrl(this.baseUrl, this.spriteUrl);

Expand Down
2 changes: 1 addition & 1 deletion src/style/colorful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import StyleBuilder from '../lib/style_builder.js';
import type { StyleRules, StyleRulesOptions } from '../lib/types.js';

export default class Colorful extends StyleBuilder<Colorful> {
public readonly name: string = 'colorful';
public readonly name: string = 'Colorful';

public defaultFonts = {
regular: 'noto_sans_regular',
Expand Down
2 changes: 1 addition & 1 deletion src/style/graybeard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Colorful from './colorful.js';

export default class Graybeard extends Colorful {
public readonly name: string = 'graybeard';
public readonly name: string = 'Graybeard';

public constructor() {
super();
Expand Down
2 changes: 1 addition & 1 deletion src/style/neutrino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import StyleBuilder from '../lib/style_builder.js';
import type { StyleRules, StyleRulesOptions } from '../lib/types.js';

export default class Neutrino extends StyleBuilder<Neutrino> {
public readonly name: string = 'neutrino';
public readonly name: string = 'Neutrino';

public defaultFonts = {
regular: 'noto_sans_regular',
Expand Down

0 comments on commit df24dd4

Please sign in to comment.