I have a reducer in react which I am testing I want to test the return value of a reduce inside the exports default function. And I'm not sure how to get access to the reduce inside that function.
MyReducer:
exports default function myReducer({
people = [0,1,2];
people.reduce(function(sum, peeps){
return sum + peeps;
}0);
return "hey";
})
In jest test:
import myReducer from 'myReducer';
expectedPeeps = 3;
expect(Something needs to go here).toEqual(expectedPeeps);
I need to figure out how to get the return value of people.reduce. I can get "hey" by just invoking myReducer(). But how do I get the return value of people.reduce in my test! ?thanks in advance peeps.
First of all, you have several syntax issues in MyReducer. I think it should look like this:
export default function myReducer() {
const people = [0,1,2];
people.reduce(function(sum, peeps){
return sum + peeps;
}, 0);
return "hey";
}
If you need to test the reduce by itself, you could move it into its own function and export it:
export function reducePeople(people) {
return people.reduce(function(sum, peeps){
return sum + peeps;
}, 0);
}
export default function myReducer() {
people = [0,1,2];
reducePeople(people);
return "hey";
}
And import and test it in your tests:
import { reducePeople } from 'myReducer';
expectedPeeps = 3;
expect(reducePeople([0,1,2])).toEqual(expectedPeeps);
Related
I would like to mock an object used inside called tested function. All I have found I another tickets was calling function in tested function, but I need to replace certain object that is used there. Is something like that possible in Jest?
util.test.js
describe('testname', function () {
it('import test', function () {
Utils.generateRandomPoints(10); // inside this function, I'd like to mock used object
});
});
utils.js
export function generateRandomPoints(arrayLength) {
const result = [];
for (let i = 0; i < 100; i++) {
// instead of "new Point" I'd like to use "new MockedPoint" for testing.
result.push(new Point(i));
}
return result;
};
const MockPoint = jest.fn();
jest.mock('path/to/Point', () => ({
Point: jest.fn().mockImplementation(MockPoint),
}));
and you can add whatever functionality to MockPoint, e.g.
const MockPoint = jest.fn((props) => console.log(props));
In this exercise I can't figure out why if I return the function I get an error, while if I return the function as a literal object it works
(module.js not works)
module.exports = {eleva : allaSeconda()}
function allaSeconda(){
function calcola(num){
return num*num
}
return calcola
}
(module.js works)
module.exports = {eleva : allaSeconda()}
function allaSeconda(){
function calcola(num){
return num*num
}
return {calcola}
}
(index.js)
const {eleva} = require('./modulo')
console.log(eleva.calcola(9))
Your syntax is a little bit convoluted, you can simply:
// no need for parenthesis in the exports.
// you export a function that elevates
module.exports = {eleva : allaSeconda}
function allaSeconda(num){
return num*num
}
Which could also be done totally inline:
// no need for parenthesis in the exports.
// you export a function that elevates
module.exports = {
eleva: function(num) {
return num * num;
}
}
And you call it simply like so:
const {eleva} = require('./modulo')
console.log(eleva(9))
Please note that your first example works, if you call eleva directly.
I am coming from vue and used to composable functions. I am trying to figure out the way to do this in svelte
So I make a js file and import store and then was trying to make a function that I could call on multiple components and act individually
swipe.js file
import { writable, derived, get } from 'svelte/store';
function createSwipe() {
const dyFromStart = writable(0)
function moveEvent(eventType, val){
console.log('moveEvent', eventType, val, get(dyFromStart))
dyFromStart.update(n => n + 1);
}
const dxScore = derived(dyFromStart, $dyFromStart => $dyFromStart + 3)
const dyScore = derived(dyFromStart, $dyFromStart => Math.round($dyFromStart + 100));
return {
moveEvent,
dxScore,
dyScore,
};
}
export const swipe = createSwipe();
then in .svelte component import function in script and decompose into subparts
<script>
import { swipe } from "$lib/swipe";
let { moveEvent, dxScore, dyScore } = swipe
</script>
<p>{$dxScore}{$dyScore}</p>
<button on:click="() => moveEvent">button</button>
Well eventually I want to turn into a swipe component hence name but trying to get fundamentals down. So I want to be able to have unique store for each component and for this if I use multiple of this .svelte component the state is shared amongst all.
And not just like three idk modal.svelte components I want to use swipe for a bunch of diff components maybe a photoViewer.svelte right just generic swipe function and use same code for all.
or would I just have to keep the state like const dyFromStart = writable(0) be just let dyFromStart = 0 in each .svelte component and pass it into a pure js function that returns results and update local .svelte variables
Adding this as the non store more pure js things I was trying but couldn't get to be reactive so accepting the answer below on store method that worked and sounds like is the correct approach
export function createSwipe() {
let dyFromStart = 0
function moveEvent(eventType, val){
console.log('moveEvent', eventType, val, dyFromStart, dxScore(), dyScore())
dyFromStart++
}
function dxScore(){ return dyFromStart + 3 }
// const dzScore = derived(dyFromStart, $dyFromStart => $dyFromStart + 3)
const dyScore = () => Math.round(dyFromStart + 100)
return {
moveEvent,
dxScore,
dyScore,
dyFromStart
};
export function createSwipe() {
let dyFromStart = 0
let dxScore = dyFromStart + 3
let dyScore = Math.round(dyFromStart + 100)
function moveEvent(eventType, val){
console.log('moveEvent', eventType, val, dyFromStart, dxScore, dyScore)
dyFromStart++
dxScore = dyFromStart + 3
dyScore = Math.round(dyFromStart + 100)
}
return {
moveEvent,
dxScore,
dyScore,
dyFromStart
};
I suppose that works fine just not reactive with $ and need to call to update a diff local var if doing that
this would seem most sveltey to me or something like it as far as composable function type style not store type
export function createSwipe() {
let dyFromStart = 0
function moveEvent(eventType, val){
console.log('moveEvent', eventType, val)
dyFromStart++
}
$: dxScore = dyFromStart + 3
$: dyScore = Math.round($dyFromStart + 100)
return {
moveEvent,
dxScore,
dyScore,
};
}
I don't understand the question fully, so I try to reiterate first what I think you want:
You want to use your swipe function in multiple places
Each usage of that swipe function should be independent of all others
If that's correct, then the answer is simple: Don't do export const swipe = createSwipe(). Delete that part and instead export the create function to use directly within your components. That way you create a new independent instance each time:
<script>
import { createSwipe } from "$lib/swipe";
let { moveEvent, dxScore, dyScore } = createSwipe()
</script>
<p>{$dxScore}{$dyScore}</p>
<button on:click="() => moveEvent">button</button>
The objective of this code is to write the rev function and make it return the following Obviously its
Maybe that's what you wanted. Since you are passing a function as a parameter you are using high order function or a decorator , hope this helps
check this here
function welcome(name) {
return `Welcome ${name}`;
}
function bye(name) {
return `Bye ${name}`;
}
function rev(wrapped) {
return function() {
const result = wrapped.apply(this,arguments);
return `${result}, ${result.split(" ").reverse().join(" ")}`
}
}
const revWelcome = rev(welcome);
const revBye = rev(bye);
console.log(revWelcome('James'))
console.log(revBye('Bond'))
Not sure if this is the right title but should be quick help.
Pretty much I keep on getting errors on testing a function because "TypeError: ParseThis.changeIt is not a function". Here's my code. What am I missing that causing this type error? Thanks!
const ParseThis = () => {
const changeIt = string => string;
return { changeIt: changeIt() }
}
Edit: More details!
Thanks for the help again
When you return your object, maybe you wanted to return the function and not the result of the call:
return { changeIt: changeIt };
or this which is more concise:
return { changeIt };
According to how you are using the translate function, I think you should export it this way:
const Translator = {
const translate = string => string;
};
if (module.exports) {
module.exports = Translator;
}
or this way:
const Translator = () => {
const translate = string => string;
return { translate };
}
if (module.exports) {
module.exports = Translator();
}
Return the function instead of calling it.
const ParseThis = () => {
const changeIt = string => string;
return { changeIt };
}
In the original post, changeIt() is a call to changeIt with no first parameter. It returns the value undefined. To return a function instead of calling it, omit the parenthesis.
Let's analyze your code.
Let's start from this:
const changeIt = string => string;
At this point, changeIt is a function that, given a parameter, it returns that a parameter.
Without an arrow function, if we should use the old classic named function, it would be like this:
function changeIt(parameter) {
return parameter;
}
What happens if you call changeIt() with no parameter? In javascript, when you pass no parameters ot a function, it's like you are passing undefined. So the function will return undefined.
Then you have this line:
return { changeIt: changeIt() }
But as we have seen, changeIt() is equal to undefined. So your code is equivalent to:
return { changeIt: undefined }
Which is clearly not a function!
What you probably meant to do, is not returning the result of function invokation, but return the function itself. So, instead that assigning changeIt(), just assign changeIt:
return { changeIt: changeIt }
Notice that you repeated twice the word changeIt, so you can get rid of this repetition and just write:
return { changeIt }
Your function is returning an object, so instead of
ParseThis.changeIt()
You should be doing something like
const a = ParseThis();
a.changeIt('some string');
But, note that even in your example, changeIt in the returning object is not a function.
Probably you are trying this
const ParseThis = () => {
const changeIt = string => string;
return { changeIt: changeIt};
}
Note that I've used { changeIt: changeIt}, setting changeIt to a reference of the inner function changeIt. And you are using { changeIt: changeIt()} setting changeIt to the value returned of the inner function changeIt. Those are two different operations.
The problem is that you are exporting a function, and not the object containing the nop function. You need to add parenthesis to your dummy:
const Translator = () => {
const translate = string => string;
return { translate };
};
if (module.exports) {
module.exports = Translator(); // add parenthesis here
}
Alternatively you could run the function you import, but I suspect that would be different from your real Translator api.