Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Nov 4, 2024
1 parent 710967c commit 1444e94
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 0 deletions.
124 changes: 124 additions & 0 deletions test/content/handler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

// @ts-nocheck

import assert from 'node:assert';
import sinon from 'sinon';
import esmock from 'esmock';

describe('contentHandler', () => {
let helixStub;
let adobeStub;
let contentHandler;

beforeEach(async () => {
helixStub = sinon.stub().resolves();
adobeStub = sinon.stub().resolves();
contentHandler = await esmock('../../src/content/handler.js', {
'../../src/content/helix-commerce.js': { handle: helixStub },
'../../src/content/adobe-commerce.js': { handle: adobeStub },
});
});

afterEach(() => {
sinon.restore();
});

it('returns 405 for non-GET methods', async () => {
const ctx = {
info: { method: 'POST' },
url: { pathname: '/content/product/test' },
};
const config = { pageType: 'product' };

const response = await contentHandler(ctx, config);
assert.equal(response.status, 405);
});

it('returns 400 if pageType is missing', async () => {
const ctx = {
info: { method: 'GET' },
url: { pathname: '/content/product/test' },
};
const config = {};

const response = await contentHandler(ctx, config);
assert.equal(response.status, 400);
});

it('returns 400 for invalid product path', async () => {
const ctx = {
info: { method: 'GET' },
url: { pathname: '/invalid/path' },
};
const config = { pageType: 'product' };

const response = await contentHandler(ctx, config);
assert.equal(response.status, 400);
});

it('returns 400 if no matching configuration found', async () => {
const ctx = {
info: { method: 'GET' },
url: { pathname: '/content/product/test' },
};
const config = {
pageType: 'product',
confMap: {},
};

const response = await contentHandler(ctx, config);
assert.equal(response.status, 400);
});

it('calls handleHelixCommerce if catalogSource is helix', async () => {
const ctx = {
info: { method: 'GET' },
url: { pathname: '/content/product/us/p/product-urlkey' },
};
const config = {
pageType: 'product',
catalogSource: 'helix',
confMap: {
'/us/p/{{urlkey}}': { some: 'config' },
},
};

await contentHandler(ctx, config);
assert(helixStub.calledOnce);
assert.deepStrictEqual(helixStub.firstCall.args[0], ctx);
assert.deepStrictEqual(helixStub.firstCall.args[1], config);
assert.equal(config.matchedPath, '/us/p/{{urlkey}}');
assert.deepStrictEqual(config.matchedPathConfig, { some: 'config' });
});

it('calls handleAdobeCommerce', async () => {
const ctx = {
info: { method: 'GET' },
url: { pathname: '/content/product/us/p/product-urlkey' },
};
const config = {
pageType: 'product',
confMap: {
'/us/p/{{urlkey}}': { some: 'config' },
},
};

await contentHandler(ctx, config);
assert(adobeStub.calledOnce);
assert.deepStrictEqual(adobeStub.firstCall.args[0], ctx);
assert.deepStrictEqual(adobeStub.firstCall.args[1], config);
assert.equal(config.matchedPath, '/us/p/{{urlkey}}');
assert.deepStrictEqual(config.matchedPathConfig, { some: 'config' });
});
});
154 changes: 154 additions & 0 deletions test/utils/admin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

// @ts-nocheck

import { strict as assert } from 'node:assert';
import sinon from 'sinon';
import { createAdminUrl, callAdmin, ADMIN_ORIGIN } from '../../src/utils/admin.js';

describe('admin utils', () => {
let fetchStub;

beforeEach(() => {
// Setup fetch stub before each test
// eslint-disable-next-line no-multi-assign
global.fetch = fetchStub = sinon.stub();
});

afterEach(() => {
// Restore all stubs after each test
sinon.restore();
});

describe('createAdminUrl', () => {
it('creates basic admin URL with required parameters', () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
};
const url = createAdminUrl(config, 'preview');

assert.equal(url.toString(), `${ADMIN_ORIGIN}/preview/adobe/blog/main`);
});

it('creates URL with custom path', () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
};
const url = createAdminUrl(config, 'preview', '/content/index');

assert.equal(url.toString(), `${ADMIN_ORIGIN}/preview/adobe/blog/main/content/index`);
});

it('handles admin version parameter', () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
adminVersion: '1.2.3',
};
const url = createAdminUrl(config, 'preview');

assert.equal(url.searchParams.get('hlx-admin-version'), '1.2.3');
});

it('handles custom search parameters', () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
};
const searchParams = new URLSearchParams();
searchParams.append('foo', 'bar');

const url = createAdminUrl(config, 'preview', '', searchParams);

assert.equal(url.searchParams.get('foo'), 'bar');
});

it('creates URL without org/site/ref when not all are provided', () => {
const config = {
org: 'adobe',
// site missing
ref: 'main',
};
const url = createAdminUrl(config, 'preview');

assert.equal(url.toString(), `${ADMIN_ORIGIN}/preview`);
});
});

describe('callAdmin', () => {
it('makes GET request by default', async () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
};

fetchStub.resolves(new Response());

await callAdmin(config, 'preview');

assert(fetchStub.calledOnce);
const [url, opts] = fetchStub.firstCall.args;
assert.equal(url.toString(), `${ADMIN_ORIGIN}/preview/adobe/blog/main`);
assert.equal(opts.method, 'get');
assert.equal(opts.headers, undefined);
assert.equal(opts.body, undefined);
});

it('makes POST request with JSON body', async () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
};

const body = { hello: 'world' };
fetchStub.resolves(new Response());

await callAdmin(config, 'preview', '', {
method: 'post',
body,
});

assert(fetchStub.calledOnce);
const [_, opts] = fetchStub.firstCall.args;
assert.equal(opts.method, 'post');
assert.deepEqual(opts.headers, { 'Content-Type': 'application/json' });
assert.equal(opts.body, JSON.stringify(body));
});

it('includes search parameters in request', async () => {
const config = {
org: 'adobe',
site: 'blog',
ref: 'main',
};

const searchParams = new URLSearchParams();
searchParams.append('test', 'value');
fetchStub.resolves(new Response());

await callAdmin(config, 'preview', '', { searchParams });

assert(fetchStub.calledOnce);
const [url] = fetchStub.firstCall.args;
assert.equal(url.searchParams.get('test'), 'value');
});
});
});

0 comments on commit 1444e94

Please sign in to comment.