Skip to content

Commit

Permalink
Add generate_title helpers
Browse files Browse the repository at this point in the history
- will be used by new set_chrome helper
  • Loading branch information
cee-chen committed Aug 5, 2020
1 parent 818f30e commit dac16d4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
generateTitle,
enterpriseSearchTitle,
appSearchTitle,
workplaceSearchTitle,
} from './generate_title';

describe('generateTitle', () => {
it('creates a pipe separated string from an array of page titles', () => {
const title = generateTitle(['Curations', 'some Engine', 'App Search']);
expect(title).toEqual('Curations | some Engine | App Search');
});
});

describe('enterpriseSearchTitle', () => {
it('automatically appends the Enterprise Search product onto the pages array', () => {
const title = enterpriseSearchTitle(['Setup Guide']);
expect(title).toEqual('Setup Guide | Enterprise Search');
});

it('can be mixed and matched', () => {
const title = enterpriseSearchTitle([appSearchTitle(['Some Page'])]);
expect(title).toEqual('Some Page | App Search | Enterprise Search');
});

it('falls back to product name', () => {
const title = enterpriseSearchTitle();
expect(title).toEqual('Enterprise Search');
});
});

describe('appSearchTitle', () => {
it('automatically appends the App Search product onto the pages array', () => {
const title = appSearchTitle(['Engines']);
expect(title).toEqual('Engines | App Search');
});

it('falls back to product name', () => {
const title = appSearchTitle();
expect(title).toEqual('App Search');
});
});

describe('workplaceSearchTitle', () => {
it('automatically appends the Workplace Search product onto the pages array', () => {
const title = workplaceSearchTitle(['Sources']);
expect(title).toEqual('Sources | Workplace Search');
});

it('falls back to product name', () => {
const title = workplaceSearchTitle();
expect(title).toEqual('Workplace Search');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
ENTERPRISE_SEARCH_PLUGIN,
APP_SEARCH_PLUGIN,
WORKPLACE_SEARCH_PLUGIN,
} from '../../../../common/constants';

/**
* Generate a document title that generally follows our breadcrumb trails
* https://github.com/elastic/kibana/blob/master/docs/development/core/public/kibana-plugin-core-public.chromedoctitle.md
*/

export type TTitle = string[];

/**
* Given an array of page titles, return a final formatted document title
* @param pages - e.g., ['Curations', 'some Engine', 'App Search']
* @returns - e.g., 'Curations | some Engine | App Search'
*/
export const generateTitle = (pages: TTitle) => pages.join(' | ');

/**
* Product-specific helpers
*/

export const enterpriseSearchTitle = (page: TTitle = []) =>
generateTitle([...page, ENTERPRISE_SEARCH_PLUGIN.NAME]);

export const appSearchTitle = (page: TTitle = []) =>
generateTitle([...page, APP_SEARCH_PLUGIN.NAME]);

export const workplaceSearchTitle = (page: TTitle = []) =>
generateTitle([...page, WORKPLACE_SEARCH_PLUGIN.NAME]);

0 comments on commit dac16d4

Please sign in to comment.