FACG2 / 200Palette

200Palette, When colors are your game!
0 stars 4 forks source link

Testing an impure function #29

Open finnhodgkin opened 7 years ago

finnhodgkin commented 7 years ago

The functions you are testing in backendScript currently rely on external data. This means that if you add data to your database at a later time it could cause your tests to fail unexpectedly. They could quite easily be converted to pure functions and then tested in a more structured, scale-able way. For example the suggest function:

function suggest(sugName) {
  var result = dbKeys.filter(function(item) {
    return item.startsWith(sugName);
  });
  return result;
}

Could be converted to:

function suggest(sugName, searchArray) {
  var result = searchArray.filter(function(item) {
    return item.startsWith(sugName);
  });
  return result;
}

Although this makes the function slightly harder to call, it allows easy exchanging of data: you could swap the database to an array of words and the function wouldn't break.

finnhodgkin commented 7 years ago

Currently if you added a colour 'redish' to the list, it would make tests fail.