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
) {
if (sentimentData !=null) {
const DataInput = [];
Object.keys(sentimentData).map(function (key, item) {
var sentimentCurrent = sentimentData[key];
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"}
});
return DataInput
}
else { return 0 }
}
The test case is as follows,
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import generateLineGraphPointsSentiment from './generateGraphPointsAnalysis';
const ExpressionData = [
{
"2021-08-21": [
{
"id": 96451,
"user_id": "xxxx",
"sentiment": 0.4,
"magnitude": 1,
"created": 1629535058,
"createdDay": "2021-08-21"
},
{
"id": 96452,
"user_id": "xxx",
"sentiment": -0.1,
"magnitude": 1,
"created": 1629535060,
"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?
Updated test data to duplicate what is going in at start, and the after the first map data is like this,
[
{
"id": 96451,
"user_id": "xxx",
"sentiment": 0.4,
"magnitude": 1,
"created": 1629535058,
"createdDay": "2021-08-21"
},
{
"id": 96452,
"user_id": "xxx",
"sentiment": -0.1,
"magnitude": 1,
"created": 1629535060,
"createdDay": "2021-08-21"
},
]
Keeping in mind this currently works when used in the line graph, which is like this from ReCharts library,
<LineChart
width={550}
height={250}
data={Sentiment}
syncId="anyId"
margin={{
top: 10,
right: 30,
left: 0,
bottom: 0,
}}
>
