Skip to content

Commit

Permalink
Merge pull request #3183 from michasik/fix/storeCode-for-category-link
Browse files Browse the repository at this point in the history
Adds storeCode to formatCategoryLink method
  • Loading branch information
patzick committed Jul 9, 2019
2 parents 327db3e + 998f36b commit 6161f63
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/modules/url/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import config from 'config'
import { localizedDispatcherRoute, localizedRoute, LocalizedRoute } from '@vue-storefront/core/lib/multistore'
import { RouteConfig } from 'vue-router/types/router';
import { RouterManager } from '@vue-storefront/core/lib/router-manager'
import { currentStoreView } from '@vue-storefront/core/lib/multistore'
import { Category } from 'core/modules/catalog-next/types/Category';

export function parametrizeRouteData (routeData: LocalizedRoute, query: { [id: string]: any } | string, storeCodeInPath: string): LocalizedRoute {
const parametrizedRoute = Object.assign({}, routeData)
Expand Down Expand Up @@ -52,8 +54,13 @@ export function normalizeUrlPath (url: string): string {
return url
}

export function formatCategoryLink (category: { url_path: string, slug: string }): string {
return config.seo.useUrlDispatcher ? ('/' + category.url_path) : ('/c/' + category.slug)
export function formatCategoryLink (category: Category, storeCode: string = currentStoreView().storeCode): string {
storeCode ? storeCode += '/' : storeCode = '';

if (category) {
return config.seo.useUrlDispatcher ? ('/' + storeCode + category.url_path) : ('/' + storeCode + 'c/' + category.slug)
}
return '/' + storeCode;
}

export function formatProductLink (
Expand Down
129 changes: 129 additions & 0 deletions core/modules/url/test/unit/formatCategoryLink.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { formatCategoryLink } from '@vue-storefront/core/modules/url/helpers';
import { Category } from '@vue-storefront/core/modules/catalog-next/types/Category';
import { currentStoreView } from '@vue-storefront/core/lib/multistore';
import config from 'config';

jest.mock('@vue-storefront/core/app', () => jest.fn());
jest.mock('@vue-storefront/core/lib/router-manager', () => jest.fn());
jest.mock('@vue-storefront/core/lib/multistore', () => ({
currentStoreView: jest.fn(),
localizedDispatcherRoute: jest.fn(),
localizedRoute: jest.fn()
}));

describe('formatCategoryLink method', () => {
let category: Category;

beforeEach(() => {
jest.clearAllMocks();
jest.mock('config', () => ({}));
(currentStoreView as jest.Mock).mockImplementation(() => ({storeCode: ''}));
category = {
path: '1/2',
is_active: true,
level: 1,
product_count: 1181,
children_count: '38',
parent_id: 1,
name: 'All',
id: 2,
url_key: 'all-2',
children_data: [],
url_path: 'all-2/women/women-20',
slug: 'all-2'
};
});

describe('with active urlDispatcher', () => {
beforeEach(() => {
config.seo = {
useUrlDispatcher: true
};
});

it('should return formatted category url_path', () => {
const result = formatCategoryLink(category);
expect(result).toEqual('/all-2/women/women-20');
});

it('should return formatted category url_path when storeCode passed as null', () => {
const result = formatCategoryLink(category, null);
expect(result).toEqual('/all-2/women/women-20');
});

it('should return formatted category url_path when storeCode passed as \'de\'', () => {
const result = formatCategoryLink(category, 'de');
expect(result).toEqual('/de/all-2/women/women-20');
});

it('should return formatted category url_path when storeCode passed as \'\'', () => {
const result = formatCategoryLink(category, '');
expect(result).toEqual('/all-2/women/women-20');
});

it('should return homepage path when category passed as \'null\'', () => {
const result = formatCategoryLink(null);
expect(result).toEqual('/');
});

describe('with default storeCode set to \'de\'', () => {
beforeEach(() => {
(currentStoreView as jest.Mock).mockImplementationOnce(() => ({storeCode: 'de'}));
});

it('should return formatted category url_path', () => {
const result = formatCategoryLink(category);
expect(result).toEqual('/de/all-2/women/women-20');
});

it('should return homepage path when category passed as \'null\'', () => {
const result = formatCategoryLink(null);
expect(result).toEqual('/de/');
});
});
});

describe('without urlDispatcher', () => {
beforeEach(() => {
config.seo = {
useUrlDispatcher: false
};
});

it('should return old path with c and category slug', () => {
const result = formatCategoryLink(category);
expect(result).toEqual('/c/all-2');
});

it('should return old path with c and category slug when storeCode passed as null', () => {
const result = formatCategoryLink(category, null);
expect(result).toEqual('/c/all-2');
});

it('should return old path with c and category slug when storeCode passed as \'de\'', () => {
const result = formatCategoryLink(category, 'de');
expect(result).toEqual('/de/c/all-2');
});

it('should return old path with c and category slug when storeCode passed as \'\'', () => {
const result = formatCategoryLink(category, '');
expect(result).toEqual('/c/all-2');
});

describe('with default storeCode set to \'de\'', () => {
beforeEach(() => {
(currentStoreView as jest.Mock).mockImplementationOnce(() => ({storeCode: 'de'}));
});

it('should return formatted category url_path', () => {
const result = formatCategoryLink(category);
expect(result).toEqual('/de/c/all-2');
});

it('should return homepage path when category passed as \'null\'', () => {
const result = formatCategoryLink(null);
expect(result).toEqual('/de/');
});
})
});
});

0 comments on commit 6161f63

Please sign in to comment.