1

Prompt, how to check up an array on? In the end result, the test should check whether the array has changed, and if yes, give an error that it can not be changed.

import { List, Set } from "immutable"
export let list1: number[] = [1, 2, 3];

export function mass(list1) {
   let listFromArray = List(list1); 
    if (listFromArray == List(list1)) {
      throw new Error("immutable");
    }
        return listFromArray; 
  };

Test code:

import {mass} from "./sum"
import { List, Set, isImmutable } from "immutable"

test('check for variability', () => {
  const list1: number[] = [1, 2, 3];
  const listFromArray = mass(list1);
  const expectedResult = List([1, 2, 3]);
  expect(expectedResult).toThrowError('Error'); 
});
skyboyer
  • 22,209
  • 7
  • 57
  • 64
deniz
  • 7
  • 2

1 Answers1

0

I think you're looking for a array comparison.

For that scenerio, you can compare the items in the array..

There are a lot of useful methods to do this, one such is to use the every method: \

var arr_1 = [1,2,3];
var arr_2 = [1,2,3];

if(arr_1.every((v,i) => v === arr_2[i])){
  alert("si");
}
else{
    alert("no")
}

Of course this is not optimised as you might want to check for length etc before hand to have early outs.

Related : How to compare arrays in JavaScript?

https://jsfiddle.net/tspqeyz3/

In the case for immutable.js, there is an equals method which will check for the mapping of values. (NodeJS example)

const { Map, is } = require('immutable')
const map1 = Map({ a: 1, b: 2, c: 3 })
const map2 = Map({ a: 1, b: 2, c: 3 })
map1 !== map2     // two different instances are not reference-equal
map1.equals(map2) // but are value-equal if they have the same values
is(map1, map2)    // alternatively can use the is() function

So in this situation you should be able to identify the equality` of the two.

Pogrindis
  • 7,755
  • 5
  • 31
  • 44
  • Thank you. How to check the value in the test? – deniz Jun 26 '18 at 09:32
  • Check that the value does not change and if an error occurs, produce an error. I was told this should be done through toThrow() – deniz Jun 26 '18 at 09:34
  • @deniz well for starter, your `toThrowError`should be lookking for `immutable` error.. as that it what you are throwing in your mass method.. IE : `.toThrowError('immutable');` – Pogrindis Jun 26 '18 at 09:40