A
A
Andrey Salnikov2016-07-30 11:45:58
JavaScript
Andrey Salnikov, 2016-07-30 11:45:58

How to test an already written site?

I am writing a website. Typical online store. Adding a product, mini cart and shopping cart. Without any fraimworks. Stupidly native. All in one file. In general, as a real work developed XD. At a certain point, everything works, but the client wants to slightly expand and change the functionality. I add a couple of functions and ... nothing works. Well, that is, part works, part works, part works wrong or not there. Before that, tests were not written and I have no idea how they are written at all. And they tell me - I had to write tests for my JS to see what works and what doesn't. And the question is - how can this be done already on a working site? All files on the server, the database is there. I looked - it was necessary to write with one of the BDD or TDD methodologies. And use various fraimworks for this. mocha for description, jusmine or chai for tests, karma for running tests in the console. Okay, but how do I attach all this to the site and my functions? There is a class, it is initialized if, for example, there is a certain class on the page.

function Class(){
  tihs._data = {
    text: 'Нажал'
  }
  this.init();
}

Class.prototype.init = function(){
  var btns = document.querySelectorAll('.btns');
  if(!!btns.length){
    for(var i = 0; i < btns.length; i++){
      btns[i].onclick = this.btn_click();
    }
  }
}

Class.prototype.btn_click = function(){
  return function(ev){
    console.info(this._data.text);
  }.bind(this)
}

This is an example code that I write for my purposes. It is sometimes inherited, sometimes it expands. But all this somehow interacts with the DOM. And how to test it? And even if the site is already posted?
Summarizing:
1) Is it possible to conduct tests with code that is written in one file and interacts closely with the DOM?
2) If not, does it need to be rewritten into modules? I read somewhere that it's not bad to use a modular approach with, for example, require.js. I used it, it's interesting.
3) If it’s impossible to test something like mine, then how should I write my website logic so that I can test it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2016-07-30
@Shshzik

Tests can be run with ANY code.
The algorithm is simple: action->result->comparison of expectation and result->conclusion.
I advise Nightmare to nightmare ..... ( www.nightmarejs.org )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question