Skip to content

Commit

Permalink
Create sample test cases in incremental-updater module
Browse files Browse the repository at this point in the history
  • Loading branch information
leungkinghin-ct committed Sep 12, 2023
1 parent 7e10d1a commit 455ee1a
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 150 deletions.
3 changes: 3 additions & 0 deletions incremental-updater/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};
8 changes: 8 additions & 0 deletions incremental-updater/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
displayName: 'Tests Javascript Application - Service',
moduleDirectories: ['node_modules', 'src'],
testMatch: ['**/tests/**/*.js?(x)', '**/?(*.)+(spec|test).js?(x)'],
testEnvironment: 'node',
verbose: true,
silent: true,
};
572 changes: 423 additions & 149 deletions incremental-updater/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions incremental-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"algoliasearch": "^4.19.1",
"body-parser": "^1.20.1",
"express": "^4.18.2",
"supertest": "^6.3.3",
"validator": "^13.7.0"
}
}
2 changes: 1 addition & 1 deletion incremental-updater/src/utils/decoder.utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const decodeToString = (encodedMessageBody) => {
const buff = new Buffer(encodedMessageBody, 'base64');
const buff = Buffer.from(encodedMessageBody, 'base64');
return buff.toString().trim();
};

Expand Down
65 changes: 65 additions & 0 deletions incremental-updater/test/route.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect, describe, afterAll, it } from '@jest/globals';
import request from 'supertest';
import server from '../src/index.js';
import { encodeJsonObject } from './utils/encoder.utils.js';

/** Reminder : Please put mandatory environment variables in the settings of your github repository **/
describe('Test event.controller.js', () => {
const storeKey = 'MY_STORE_KEY'; // Specify the key of Commercetools Store for product synchronization

it(`POST /`, async () => {
let response = {};
// Send request to the connector application with following code snippet.

response = await request(server).post(`/`);

expect(response).toBeDefined();
});

it(`POST /deltaSync with empty payload`, async () => {
let response = {};
// Send request to the connector application with following code snippet.
let payload = {};
response = await request(server).post(`/deltaSync`).send(payload);

expect(response).toBeDefined();
expect(response.statusCode).toEqual(400);
});

it(`POST /deltaSync with correct message in payload body`, async () => {
let response = {};
// Send request to the connector application with following code snippet.
// Following incoming message data is an example. Please define incoming message based on resources identifer in your own Commercetools project
const incomingMessageData = {
notificationType: 'ResourceUpdated',
projectKey: 'connect-search-template',
resource: { typeId: 'product', id: 'dummy-product-id' },
resourceUserProvidedIdentifiers: {
key: 'dummy-product-key',
slug: { 'de-DE': 'dummy-product-slut' },
},
version: 11,
oldVersion: 10,
modifiedAt: '2023-09-12T00:00:00.000Z',
};

const encodedMessageData = encodeJsonObject(incomingMessageData);
let payload = {
message: {
data: encodedMessageData,
},
};
response = await request(server).post(`/deltaSync`).send(payload);

expect(response).toBeDefined();
expect(response.statusCode).toEqual(204);
});

afterAll(() => {
// Enable the function below to close the application on server once all test cases are executed.

if (server) {
server.close();
}
});
});
9 changes: 9 additions & 0 deletions incremental-updater/test/utils/encoder.utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const encodeString = (message) => {
const buff = Buffer.from(message);
return buff.toString('base64').trim();
};

export const encodeJsonObject = (messageBody) => {
const message = JSON.stringify(messageBody);
return encodeString(message);
};

0 comments on commit 455ee1a

Please sign in to comment.