Testing redux-saga with multiple selects - jestjs

I have a saga with select functions that should take couple of information from state and use them to go to new url.
CODE FOR SAGA:
export function* appRedirectToNewTopic() {
const getCreatingNewTopicError = yield select(getCreatingTopicError);
const newTopicId = yield select(getNewTopicId);
const groupId = yield select(getGroupId);
if (isEmpty(getCreatingNewTopicError)) { // this is lodash isEmpty
yield put(push(getRouteUrl(routerUrls.TOPIC_CHAT.url, {
groupId,
topicId: newTopicId,
})));
} // TODO add here when notification is made
}
CODE FOR SAGA TEST:
describe('appRedirectToNewTopic', () => {
const action = appCreateNewTopicFinishedAction();
const generator =
cloneableGenerator(appRedirectToNewTopic)(action);
const expectedGroupId = {
apiGroups: {
groupInfo: {
groupInfo: {
id: 466,
},
},
},
};
const expectedTopicId = {
apiTopics: {
newTopicId: 22466,
},
};
let topicId;
let groupId;
it('should return empty error', () => {
expect(generator.next().value)
.toEqual(select(getCreatingTopicError));
});
it('should return new topicId', () => {
topicId = generator.next(expectedTopicId).value;
expect(topicId)
.toEqual(select(getNewTopicId));
});
it('should return groupId', () => {
groupId = generator.next(expectedGroupId).value;
expect(groupId)
.toEqual(select(getGroupId));
});
it('should redirect user to new topic screen', () => {
expect(generator.next().value)
.toEqual(put(push(getRouteUrl(routerUrls.TOPIC_CHAT.url, {
groupId,
topicId,
}))));
});
});
The error that I get is in should redirect user to new topic and the error is Expected value to equal:
{"##redux-saga/IO": true, "PUT": {"action": {"payload": {"args": ["/chat/[object Object]/[object Object]"], "method": "push"}, "type": "##router/CALL_HISTORY_METHOD"}, "channel": null}}
Received:
undefined

The value you are passing to the next method is what the current yield is supposed to yield, not the one you are testing with equal after that. Try this:
describe('appRedirectToNewTopic', () => {
const action = appCreateNewTopicFinishedAction();
const generator =
cloneableGenerator(appRedirectToNewTopic)(action);
const expectedGroupId = {
apiGroups: {
groupInfo: {
groupInfo: {
id: 466,
},
},
},
};
const expectedTopicId = {
apiTopics: {
newTopicId: 22466,
},
};
let topicId;
let groupId;
it('should return empty error', () => {
expect(generator.next().value)
.toEqual(select(getCreatingTopicError));
});
it('should return new topicId', () => {
topicId = generator.next().value;
expect(topicId)
.toEqual(select(getNewTopicId));
});
it('should return groupId', () => {
groupId = generator.next(expectedTopicId).value;
expect(groupId)
.toEqual(select(getGroupId));
});
it('should redirect user to new topic screen', () => {
expect(generator.next(expectedGroupId).value)
.toEqual(put(push(getRouteUrl(routerUrls.TOPIC_CHAT.url, {
groupId,
topicId,
}))));
});
});

Related

Jest Mockup ldap in nodejs

Here is my AD class code I am using ldapjs library and trying to mock its add method but facing some warning which cause code coverage issue
const ldap = require('ldapjs');
class AD {
/**
* Create Connection Active Directory
*/
async createConnection(){
return new Promise((resolve, reject) => {
var options = {
'rejectUnauthorized': false,
};
this.ADConnnection = ldap.createClient({
url: [
process.env.LDAP_SERVER_1 + ':' + process.env.LDAP_SERVER_PORT,
],
reconnect: true,
tlsOptions: options
});
this.ADConnnection.on('error', (err) => {
reject(err)
})
this.ADConnnection.bind(this.ldapUsername, this.ldapPassword, async (err) => {
if (err) {
reject(err)
}
});
resolve(true)
});
}
/**
* Create Record in Active Directory
* #param {*} oRequest
* #param {*} oEntry
*/
async create(oRequest, oADCreateUserDT) {
const sUsername = oRequest.vpnUsername;
this.adOu = oRequest.apiUser;
let oEntry = oADCreateUserDT.ADCreateUserData();
if(oEntry.hasOwnProperty('msRADIUSFramedIPAddress')){
this.adOu = ADConstant.AD_PARAMS.OU_DED;
oADCreateUserDT.setTitle(ADConstant.AD_PARAMS.OU_DED);
oEntry = oADCreateUserDT.ADCreateUserData();
}
return new Promise(async (resolve, reject) => {
this.ADConnnection.add('cn=' + sUsername + ',ou=' + this.adOu + ',' + this.dc, oEntry, (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
await this.closeConnection()
});
}
async closeConnection() {
this.ADConnnection.unbind(err => {
if (err) {
reject(err)
}
}, () => {
this.ADConnnection.destroy();
});
}
}
module.exports = AD;
Now this is my test class (I am using jest for nodejs testing)
const AD = require("../../app/libraries/AD");
const ldap = require('ldapjs');
jest.mock('ldapjs', () => {
return jest.fn().mockImplementation(() => {
return {
createClient: jest.fn(),
add: jest.fn(() => Promise.resolve(true)),
}
})
});
const isDedicatedIP = true;
const oRequest = { uuid: "vpn00s01" }
const oEntry = { UserAccountControl: ADConstant.AD_PARAMS.AD_ACC_CONTROL_ENABLE }
const oSearch = { attributes: ["cn", "Accountexpires", "UserAccountControl"], scope: "sub", filter: "" }
describe('should test all cases of ldap function', () => {
test('should test constructor', async () => {
const oAD = new AD()
const response = oAD
expect(JSON.stringify(response)).toBe(JSON.stringify({ "dc": "dc=undefined,dc=undefined", "adOu": "purevpn", "objectclass": "user" }));
});
test('should set Adu', async () => {
const oAD = new AD()
const response = oAD.setAdOu(isDedicatedIP)
expect(JSON.stringify(response)).toBe(JSON.stringify({}));
});
test('should test create form ldap', async () => {
const oAD = new AD()
const response = oAD.create(oRequest, oADCreateUserDT)
expect(JSON.stringify(response)).toBe(JSON.stringify({}));
});
});
While running this jest test facing this issue
I don't understand how to mock my ldapjs methods. Even after adding in in mock implementation still having the same issue

Jest - How to mock aws-sdk sqs.receiveMessage methode

I try mocking sqs.receiveMessage function which imported from aws-sdk.
Here is my code(sqsHelper.js):
const AWS = require("aws-sdk");
export default class SqsHelper {
static SqsGetMessagesTest = () => {
const sqs = new AWS.SQS({
apiVersion: serviceConfig.sqs.api_version,
region: serviceConfig.sqs.region,
});
const queueURL =
"https://sqs.us-west-2.amazonaws.com/<1234>/<4567>";
const params = {
AttributeNames: ["SentTimestamp"],
MaxNumberOfMessages: 10,
MessageAttributeNames: ["All"],
QueueUrl: queueURL,
VisibilityTimeout: 20,
WaitTimeSeconds: 20,
};
return new Promise((resolve, reject) => {
sqs.receiveMessage(params, async (recErr, recData) => {
if (recErr) {
reject(recErr);
} else if (recData.Messages) {
console.info(`Message count: ${recData.Messages.length}`);
resolve(recData.Messages);
}
});
});
};
}
And here is the test file(sqsHelper.test.js):
import SqsHelper from "../../helpers/sqsHelper.js";
import { SQS } from "aws-sdk";
const dumyData = { Messages: [{ name: "123", lastName: "456" }] };
const sqs = new SQS();
describe("Test SQS helper", () => {
test("Recieve message", async () => {
jest.spyOn(sqs, 'receiveMessage').mockReturnValue(dumyData);
// check 1
const res1 = await sqs.receiveMessage();
console.log(`res: ${JSON.stringify(res1, null, 2)}`)
expect(res1).toEqual(dumyData);
// check 2
const res2 = await SqsHelper.SqsGetMessagesTest();
console.log(`res2: ${JSON.stringify(res2, null, 2)}`);
expect(res2).toBe(dumyData);
});
});
The problem is that on the first check( which i call the function directly from the test file) i can see that the receiveMessage has been mocked and the results is as expected.
But on the second check(which the function called from the second module "sqsHelper.js") looks that the mock function doe's work and the originalreceiveMessage has been called and it still ask me about credentials.
This is the error:
InvalidClientTokenId: The security token included in the request is
invalid.
what I'm doing wrong?
Thanks
The receiveMessage should trigger a callback that comes in the params. receiveMessage does not return a Promise
Try something like this:
const dummyData = { Messages: [{ name: "123", lastName: "456" }] };
const mockReceiveMessage = jest.fn().mockImplementation((params, callback) => callback("", dummyData));
jest.mock("aws-sdk", () => {
const originalModule = jest.requireActual("aws-sdk");
return {
...originalModule,
SQS: function() { // needs to be function as it will be used as constructor
return {
receiveMessage: mockReceiveMessage
}
}
};
})
describe("Test SQS helper", () => {
test("Recieve message", async () => {
const res = await SqsHelper.SqsGetMessagesTest();
expect(res).toBe(dummyData.Messages);
});
test("Error response", async () => {
mockReceiveMessage.mockImplementation((params, callback) => callback("some error"));
await expect(SqsHelper.SqsGetMessagesTest()).rejects.toEqual("some error");
});
});

How to write test jest of async/await is toHaveBeenCalled?

`Trying to run a test for the following code using Jest.I mocked the function and checked if it runs with the desired intent. I think the error is in the promise but I can't find a solution. Can you help me?
Why redis.set not call
i get error
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
mailparser.js
mail parser will call with mgs(‘body’,(stream)=>{parser})
const parser = async (stream) => {
try {
const mail = await simpleParser(stream)
const isAvailable = await redis.set(
[`mail_message_id_${mail.messageId}`],
[`${mail.messageId}`],
{
flag: 'NX',
expiry: 60 * 30,
}
)
if (!isAvailable) {
return console.log(`${mail.messageId} is already processed.`)
}
} catch (err) {
console.error(err, 'failed to parser mail')
}
}
Here my unit Ttesting
const stream = 'stream'
const mockParser = {
simpleParser: jest.fn(),
}
const mockRedis = {
set: jest.fn(),
}
beforeAll(() => {
jest.mock('./redis', () => {
return {
set: mockRedis.set,
}
})
jest.mock('mailparser', () => {
return {
simpleParser: mockParser.simpleParser,
}
})
jest.fn().mockImplementation(() => mockMgs)
})
beforeEach(() => {
mockMgs.on.mockImplementationOnce((event, fn) => {
fn(stream)
})
})
afterEach(async () => {
await jest.clearAllMocks()
})
describe('mailparser', () => {
test('should call redis.set with params correctly', async () => {
const mailparser = require('./mailparser')
await mockParser.simpleParser.mockImplementation(() =>
Promise.resolve(mail) //mail is object data
)
mockRedis.set.mockImplementationOnce(() => Promise.resolve('OK'))
mailparser(mockMgs, 1)
expect(mockRedis.set).toHaveBeenCalled()
})

Mock node js Controller function - getSubscriber which calling to service

I have developed the node js code as MVC architecture. The folder structure is Controller --> service -> model. And I have tried to write unit testing for the following code. Unfortunately, I couldn't mock the service function. So please help me to resolve it.
Controller
const SubscriberService = require('../../services/subscriber/subscriberService')
const response = require("../../config/response");
const constant = require('../../config/constant');
const SubscriberAnswerService = require('../../services/subscriberAnswer/subscriberAnswerService');
const path = require('path');
class SubscriberController {
constructor() {
this.subscriberService = new SubscriberService();
this.subscriberAnswerService = new SubscriberAnswerService();
}
async getSubscriber(req, res) {
try {
var { userId } = req;
const user = await this.subscriberService.findByUserId(userId);
if (user != null) {
res.send(response.res(true, constant.MSG.USER_DETAILS, user))
} else {
res.status(404).send(response.res(false, constant.MSG.USER_NOT_FOUND));
}
} catch (error) {
res.status(constant.RESPONSE.INTERNAL_ERROR.CODE)
.send(response.res(false, error.message))
}
}
}
Service
async findByUserId(id) {
const user = await Subscriber.findOne({ where: { id: id, status: 1 } });
return user;
}
Unit Testing Code
describe("Test SubscriberController", () => {
it("Test getsubscriber", async () => {
req.userId = 1;
jest.spyOn(subscriberService, "findByUserId").mockReturnValue(subscriberResponse);
await subscriberController.getSubscriber(req, res);
expect(res.statusCode).toBe(500);
});
});
Issue: I have mocked the service function which findByUserId but it does not work. It is given the following error.
error TypeError: Cannot read property 'findOne' of undefined
Please give the solution to mock findByUserId function.
Subscriber.Controller.test.js
const subscriberModel = require("../src/models/subscriber/subscriberModel");
const SubscriberService = require("../src/services/subscriber/subscriberService");
const SubscriberController = require("../src/controllers/Subscriber/subscriberController");
const subscriberController = new SubscriberController();
const subscriberService = new SubscriberService();
const httpMocks = require("node-mocks-http");
jest.mock("../src/models/subscriber/subscriberModel");
beforeEach(() => {
jest.resetAllMocks();
req = httpMocks.createRequest();
res = httpMocks.createResponse();
next = jest.fn();
jest.resetAllMocks();
subscriberModel.findOne = jest.fn();
});
// subscriberService.findByUserId = jest.fn();
const subscriberResponse = {
id: 1,
name: 'Sandun',
msisdn: '94704377575',
otp: '1234',
deleted: 0,
attempts: 0,
img_url: 'https://'
}
jest.mock('../src/models/subscriber/subscriberModel', () => () => {
const SequelizeMock = require("sequelize-mock");
let dbMock = new SequelizeMock();
let subscriberMock = dbMock.define('subscribers', {
id: 1,
name: 'Sandun',
msisdn: '94704377575',
otp: '1234',
deleted: 0,
attempts: 0,
img_url: 'https://'
});
let groupMock = dbMock.define('winner', {});
subscriberMock.belongsTo(groupMock);
subscriberMock.hasMany();
});
// This test shows how the constructor can be mocked, and how to spy on passed parameters.
describe("Test SubscriberController", () => {
it("Test getsubscriber", async () => {
req.userId = 1;
jest.spyOn(subscriberService, "findByUserId").mockReturnValue(subscriberResponse);
await subscriberController.getSubscriber(req, res);
expect(res.statusCode).toBe(200);
});
});

Node JS Promise.all and forEach in nested Array

In a Node app with typescript, i need to iterate through a array and i call an asynchronous function inside the loop to get informations about the related items for each item in the array. The function is called for each related item to get its title in relatedItems array.
I'm able to retrieve promises for the 2nd level array (relatedItems) but not sure how to implement a then once top level finishes as well.
How to reach my goal with promise.all.
var Inputarray = [
{ Category: "cat1"
relatedItems: [
{id: "1"},
{id: "2"},
{id: "3"}
]
},
{
Category: "cat2"
relatedItems: [
{id: "1"},
{id: "2"},
{id: "3"}
]
}
];
var wantedresult= [
{ Category: "cat1"
relatedItems: [
{Title: "xxx"},
{Title: "yyy"},
{Title: "zzz"}
]
},
{
Category: "cat2"
relatedItems: [
{Title: "ttt"},
{Title: "kkk"},
{Title: "mmm"}
]
}
];
private GetAllRelattedItems(data: IListItem[]): any{
let rendredItems: RelatedItem[] = new Array();
data.forEach(item => {
let relatedItemsinfos : relatedItemInfos[]=item.Children;
let localTab:relatedItem[]=new Array();
let newItem = {
Category:item.Category,
Children: []
};
var promises = [];
relatedItemsinfos.forEach(relatedItemsInfositem =>{
promises.push(this.GetRelatedItem(relatedItemsInfositem.WebId,relatedItemsInfositem.ListId,relatedItemsInfositem.ItemId));
});
Promise.all(promises).then(function(response) {
response.forEach(obj=>{
let newNode: relatedItem ={
Title :Obj.Title,
};
newItem.Children.push(newNode);
});
rendredItems.push(newItem);
});
});
}
private GetRelatedItem(id:string) : Promise<relatedItem> {
return new Promise((resolve) => {
pnp.sp.site.openWeb()
.then(web => {
//this.getWeb()
//.then((web) => {
return web.web.lists.getList().get(); //w.web.lists.getById("").get().then(r => {
})
.then((list) => {
return this.getItem(list,id);
})
.then((item) => {
resolve(item);
});
});
}
You should use Promise.all at the top level and return the promise for each item in the data array:
private GetAllRelattedItems(data: IListItem[]): any {
//..
let allData = data.map(item => {
//..
return Promise.all(promises).then(function (response) {
//..
});
});
Promise.all(allData).then (_=> { /* After all data is retrieved */})
}
Since you are using Typescript a better approach would be to take advantage of async/await to make the code more readable:
private async GetAllRelattedItems(data: IListItem[]): Promise<RendredItem[]> {
let allData = data.map(async item => {
let relatedItemsinfos: relatedItemInfos[] = item.Children;
let localTab: relatedItem[] = new Array();
let newItem = {
Category: item.Category,
Children: []
};
var response = await Promise.all(relatedItemsinfos.map(relatedItemsInfositem => {
return this.GetRelatedItem(relatedItemsInfositem.WebId);
}));
newItem.Children = response.map(entry => ({
Title: entry.value[0].Title,
FileLeafRef: entry.value[0].FileLeafRef,
Modified: entry.value[0].Modified,
FileRef: entry.value[0].FileRef,
FieldValuesAsText: {
FileRef: entry.value[0].FileRef,
}
}));
return newItem;
});
var result = await Promise.all(allData);
// After all results are done
return result
}
private async GetRelatedItem(id: string): Promise<{ value: relatedItem[] }> {
const web = await pnp.sp.site.openWeb()
const list = await web.web.lists.getList().get(); //w.web.lists.getById("").get().then(r => {
return await this.getItem(list,id);
}
// Placeholder
public getItem(list: any[], id: string ): Promise<{ value: relatedItem[] }>{
return Promise.resolve({
value : []
});
}
Note I guessed some of the types based on usage, you should review to make sure they are correct.
private async GetAllRelattedItems(data: IListItem[]): Promise<RendredItem[]> {
let allData = data.map(async item => {
let relatedItemsinfos: relatedItemInfos[] = item.Children;
let localTab: relatedItem[] = new Array();
let newItem = {
Category: item.Category,
Children: []
};
var response = await Promise.all(relatedItemsinfos.map(relatedItemsInfositem => {
return this.GetRelatedItem(relatedItemsInfositem);
}));
newItem.Children = response.map(entry => ({
Title: entry.value[0].Title,
FileLeafRef: entry.value[0].FileLeafRef,
Modified: entry.value[0].Modified,
FileRef: entry.value[0].FileRef,
FieldValuesAsText: {
FileRef: entry.value[0].FileRef,
}
}));
return newItem;
});
var result = await Promise.all(allData);
// After all results are done
return result
}
private async GetRelatedItem(relatedItemsInfositem) : Promise<any> {
const web = await pnp.sp.site.openWebById(relatedItemsInfositem.WebId);
const list = await web.web.lists.getById(relatedItemsInfositem.ListId).get();
return await this.getItem(list,relatedItemsInfositem.ItemId);
// here i would like to call GetItemSize that is async and that take return the size as a number but i would like to return the whole item model
}
public getItem(list: any, itemId: string): Promise<any>{
let url: string;
if(list.ParentWebUrl !='/'){
url= list.ParentWebUrl + "/_api/web/lists/getbytitle('"+list.Title+"')/items?$select=FileLeafRef,FileRef,Title,File/ServerRelativeUrl,Modified,FieldValuesAsText/FileRef&$expand=File&$expand=FieldValuesAsText&$filter=Id eq "+itemId;
}
else url="/_api/web/lists/getbytitle('"+list.Title+"')/items?$select=FileLeafRef,FileRef,Title,File/ServerRelativeUrl,Modified,FieldValuesAsText/FileRef&$expand=FieldValuesAsText&$expand=File&$filter=Id eq "+itemId;
return this.context.spHttpClient.get(url,SPHttpClient.configurations.v1).then((response: Response)=>{
return response.json();
});
}
private async GetItemSize(title: string): Promise<relatedItem>{
let url:string ;
let response‌​ = await this.context.spHttpClient.get(url ,SPHttpClient.configuration‌​s.v1);
// Asuming the json has the shape of relatedItem : Here the response is just a number and not shaped RelatedItem the goal is to return the whole relatedItem with size information.
return <relatedItem>response.json(); }

Resources