Trying to write a unit test for the below function.
I’m getting an error of
TypeError: sentimentCurrent.map is not a function
65 | console.log("sentimentData:",sentimentCurrent,typeof sentimentCurrent);
66 |
> 67 | sentimentCurrent.map(function (k, num) {
| ^
68 | let dateCreated = new Date(k.created * 1000);
69 |
70 | DataInput.push({
The actual function is below, it takes in raw data and then prepares it to be used in a linegraph essentially.
export default function generateLineGraphPointsSentiment(
sentimentData
) {
console.log("sentimentData:",sentimentData,typeof sentimentData);
if (sentimentData !=null) {
const DataInput = [];
Object.keys(sentimentData).map(function (key, item) {
var sentimentCurrent = sentimentData[key];
console.log("sentimentData:",sentimentCurrent,typeof sentimentCurrent);
sentimentCurrent.map(function (k, num) {
let dateCreated = new Date(k.created * 1000);
DataInput.push({
created: dateCreated,
sentiment: k.sentiment,
magnitude: k.magnitude,
date: k.createdDay,
});
});
// {created: 1601820360, sentiment: -0.1, magnitude: 0.1, createdDay: "2020-10-05"}
});
The test case is as follows,
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import generateLineGraphPointsSentiment from './generateGraphPointsAnalysis';
const SentimentData = [
{
id: 96452,
user_id: 'asdasd',
sentiment: -0.1,
magnitude: 1,
created: 1629535060,
createdDay: '2021-08-21',
},
{
id: 96453,
user_id: 'asdasd',
sentiment: 1,
magnitude: 1,
created: 1629535063,
createdDay: '2021-08-21',
},
];
// const setSelectedDate = jest.fn();
describe('Generates Graph Points Sentiment', () => {
it('should show loader for the LineGraphs graph if no data is present', () => {
generateSentimentLineGraph = generateLineGraphPointsSentiment(SentimentData);
console.log(generateSentimentLineGraph,SentimentData,typeof SentimentData);
expect(generateSentimentLineGraph).toHaveReturned();
});
});
Any ideas on what is wrong with my code? It works in dev currently, not sure why it’s failing this test now? I think it has something to do with either the type of data being processed, or how I’m using the map functions. Perhaps I can simplify the first map function?