Skip to content

Commit

Permalink
[2256] Add unit tests (#75)
Browse files Browse the repository at this point in the history
* update unit tests - content package

* remove not needed lib cross-fetch

* Update tests in content and search packages

* Update tests for content and search packages using chai and nock

* Rollback objectId value in content mock-responses

* content and redux unit tests updates #2256

* Redux/util package unit tests

* Added unit tests to ice package

* Added unit tests to class package

* Add prettier to project

* Prettify sdk tests

* Update @types/mocha from v^7.0.1 to v^10.0.6

* Add missing lib in .mocharc.json #2256

* Update unit tests #2256

* Update tests description comments #2256

* Update unit tests and comments #2256

* yarn.lock update #2256

* Update tests to equal expected values #2256

* Remove optional arguments and leave defaults, update redux actions in tests #2256

* Use values from crafterConfig for endpoints, baseUrl and crafterSite in requests #2256

* Use values from crafterConfig for endpoints, baseUrl and crafterSite in requests - search tests #2256
  • Loading branch information
jvega190 authored May 14, 2024
1 parent 0cfee97 commit 1d6806a
Show file tree
Hide file tree
Showing 27 changed files with 3,159 additions and 1,071 deletions.
4 changes: 2 additions & 2 deletions default.opts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--require assert
--require mocha
--requier xhr-mock
--require xhr-mock
--require ts-node/register
--require tsconfig-paths/register
--require tsconfig-paths/register
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
],
"description": "Crafter CMS SDK for JavaScript",
"scripts": {
"build": "./build.sh"
"build": "./build.sh",
"prettier:all": "prettier -w packages/*/src/**/*.ts prettier -w packages/*/test/**/*.ts",
"prettier:packages": "prettier -w packages/*/src/**/*.ts",
"prettier:tests": "prettier -w packages/*/test/**/*.ts"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +29,7 @@
},
"homepage": "https://github.com/craftercms/craftercms-sdk-js#readme",
"devDependencies": {
"prettier": "^3.0.3",
"react": "^18.2.0",
"typescript": "^5.3.3"
},
Expand Down
10 changes: 10 additions & 0 deletions packages/classes/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"loader": "ts-node/esm",
"spec": ["test/*.spec.ts"],
"require": ["assert", "mocha", "ts-node/register"],
"node-option": [
"no-experimental-fetch",
"loader=ts-node/esm"
]
}

13 changes: 10 additions & 3 deletions packages/classes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,25 @@
"rollup": "../../node_modules/rollup/bin/rollup -c rollup.config.js",
"build:ts": "tsc -p tsconfig.build.json",
"clean:ts": "../../node_modules/rimraf/bin.js ts-out",
"build": "npm run build:ts && npm run rollup && npm run clean:ts"
"build": "npm run build:ts && npm run rollup && npm run clean:ts",
"test": "mocha"
},
"dependencies": {
"@craftercms/models": "0.0.0-PLACEHOLDER",
"@craftercms/utils": "0.0.0-PLACEHOLDER",
"query-string": "^7.1.3"
"query-string": "^7.1.3",
"url-search-params-polyfill": "^5.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"chai": "^4.3.10",
"mocha": "^10.2.0",
"nock": "^13.3.4",
"node-fetch": "2",
"rollup": "^2.79.1",
"rollup-plugin-sourcemaps": "^0.5.0",
"terser": "^5.30.1"
"terser": "^5.30.1",
"xhr2": "^0.2.1"
}
}
127 changes: 127 additions & 0 deletions packages/classes/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (C) 2007-2023 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

import 'mocha';
import { crafterConf, httpGet, httpPost } from '@craftercms/classes';
import { expect } from 'chai';
import { beforeEach } from 'mocha';
import * as nock from 'nock';
import * as xhr2 from 'xhr2';

// @ts-ignore - Setting global XMLHttpRequest for testing (not available on node)
global.XMLHttpRequest = xhr2.XMLHttpRequest;

// https://github.com/nock/nock/issues/2397
import fetch, { Headers, Request, Response } from 'node-fetch';

if (!globalThis.fetch) {
(globalThis as any).fetch = fetch;
(globalThis as any).Headers = Headers;
(globalThis as any).Request = Request;
(globalThis as any).Response = Response;
}

describe('CrafterCMS Classes', () => {
beforeEach(() => {
// replace the real XHR object with the mock XHR object before each test
if (!nock.isActive()) {
nock.activate();
}
});
// put the real XHR object back and clear the mocks after each test
afterEach(() => nock.cleanAll());

describe('ConfigManager', () => {
// Tests that crafterConfig returns a default configuration when no configuration is set.
// Values from the default configuration are mostly empty. Actual values are tested in next test.
it('Should have a default configuration', () => {
const defaultConfig = crafterConf.getConfig();
expect(defaultConfig).to.be.an('object');
expect(defaultConfig).to.have.property('site');
expect(defaultConfig).to.have.property('baseUrl');
expect(defaultConfig).to.have.property('endpoints');
expect(defaultConfig).to.have.property('contentTypeRegistry');
expect(defaultConfig).to.have.property('headers');
});
// Tests that after setting a new configuration, the provided values are set.
// e.g. an endpoint value should be different from the default value.
it('Should be able to set a new configuration', () => {
const newConfig = {
baseUrl: 'http://localhost:8080',
site: 'editorial',
searchId: 'test_search_id',
endpoints: {
GET_ITEM_URL: '/new_api/content_store/item.json',
GET_DESCRIPTOR: '/new_api/content_store/descriptor.json',
GET_CHILDREN: '/new_api/content_store/children.json',
GET_TREE: '/new_api/content_store/tree.json',
GET_NAV_TREE: '/new_api/navigation/tree.json',
GET_BREADCRUMB: '/new_api/navigation/breadcrumb.json',
TRANSFORM_URL: '/new_api/url/transform.json',
SEARCH: '/new_api/search/search.json'
}
};
crafterConf.configure(newConfig);
const updatedConfig = crafterConf.getConfig();
expect(updatedConfig.site).to.equal(newConfig.site);
expect(updatedConfig.baseUrl).to.equal(newConfig.baseUrl);
expect(updatedConfig.endpoints.GET_ITEM_URL).to.equal(newConfig.endpoints.GET_ITEM_URL);
expect(updatedConfig.endpoints.GET_DESCRIPTOR).to.not.equal('/api/1/content_store/descriptor.json');
});
// Tests that the mix method returns a mix of the current configuration (set at the previous tes) and the new values
// set at this test.
it('Should return a mix of the current config and the provided config', () => {
const mixConfig = crafterConf.mix({
baseUrl: 'http://localhost:3000',
site: 'test_site'
});
expect(mixConfig.site).to.equal('test_site');
expect(mixConfig.site).to.not.equal('editorial');
expect(mixConfig.baseUrl).to.equal('http://localhost:3000');
expect(mixConfig.endpoints.GET_DESCRIPTOR).to.equal('/new_api/content_store/descriptor.json');
expect(mixConfig.endpoints.GET_DESCRIPTOR).to.not.equal('/api/1/content_store/descriptor.json');
});
});

describe('SDKService', () => {
describe('httpGet', () => {
// Test the httpGet method with a mocked GET request.
it('Should return a response from the GET request', (done) => {
nock('http://localhost:8080').get('/api/1/test/getItem').query({ id: 1 }).reply(200, { id: 1, name: 'test' });

httpGet('http://localhost:8080/api/1/test/getItem?id=1').subscribe((response) => {
expect(response).to.not.be.null;
expect(response).to.be.an('object');
expect(response).to.deep.equal({ id: 1, name: 'test' });
done();
});
});
});

describe('httpPost', () => {
// Test the httpPost method with a mocked POST request.
it('Should return a response from the POST request', (done) => {
nock('http://localhost:8080').post('/api/1/test/addItem').reply(200, { result: 'success', id: 1 });

httpPost('http://localhost:8080/api/1/test/addItem', { id: 1, name: 'test' }).subscribe((response) => {
expect(response).to.be.an('object');
expect(response).to.deep.equal({ result: 'success', id: 1 });
done();
});
});
});
});
});
9 changes: 9 additions & 0 deletions packages/content/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"loader": "ts-node/esm",
"spec": ["test/*.spec.ts"],
"require": ["assert", "mocha", "ts-node/register"],
"node-option": [
"no-experimental-fetch",
"loader=ts-node/esm"
]
}
11 changes: 8 additions & 3 deletions packages/content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"build:ts": "tsc -p tsconfig.build.json",
"clean:ts": "../../node_modules/rimraf/bin.js ts-out",
"build": "npm run build:ts && npm run rollup && npm run clean:ts",
"test": "../../node_modules/mocha/bin/mocha --opts ../../default.opts \"test/*.spec.ts\""
"test": "mocha"
},
"dependencies": {
"@craftercms/classes": "0.0.0-PLACEHOLDER",
Expand All @@ -33,11 +33,16 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/mocha": "^7.0.2",
"mocha": "^7.2.0",
"@types/mocha": "^10.0.6",
"chai": "^4.3.8",
"mocha": "^10.2.0",
"nock": "^13.3.3",
"node-fetch": "2",
"rollup": "^2.79.1",
"rollup-plugin-sourcemaps": "^0.5.0",
"terser": "^5.30.1",
"ts-node": "^10.9.1",
"whatwg-fetch": "^3.6.19",
"xhr-mock": "^2.5.1"
}
}
Loading

0 comments on commit 1d6806a

Please sign in to comment.