<commit_before>var Fresh = require('../build/fresh.js'),
    React = require('react');

describe("Components implementing the DataManager mixin", function() {

  var DataManagerSpec = {
    mixins: [Fresh.mixins.DataManager],
    render: function() {
      return React.DOM.span(null, 'nada');
    }
  };

  it("should fetch data if a 'data' prop is set", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent({data: 'url?query=string'});

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(Fresh.mixins.DataManager
           .fetchDataFromServer.callCount).toEqual(1);
  });

  it("shouldn't fetch data if a 'data' prop isn't set", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent({});

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(Fresh.mixins.DataManager
           .fetchDataFromServer.callCount).toEqual(0);
  });

  it("should populate state.data with fetched data", function() {
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent({});

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    componentInstance.receiveDataFromServer({
      name: '<NAME>',
      age: 42
    });
    expect(componentInstance.state.data).toEqual({
      name: '<NAME>',
      age: 42
    });
  });
});<commit_msg>Add tests for initialData #4<commit_after>var Fresh = require('../build/fresh.js'),
    React = require('react'),
    _ = require('underscore');

describe("Components implementing the DataManager mixin", function() {

  var DataManagerSpec = {
    mixins: [Fresh.mixins.DataManager],
    render: function() {
      return React.DOM.span(null, 'nada');
    }
  };

  it("should default initial data to an empty object", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent();

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(componentInstance.state.data).toEqual({});
    // Since expect([]).toEqual({}) returns true we had to know for sure that
    // the data object is a plain one
    expect(JSON.stringify(componentInstance.state.data)).toEqual('{}');
  });

  it("should override initial data to an empty array", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var InitialDataSpec = _.extend({initialData: []}, DataManagerSpec),
        DataManagerComponent = React.createClass(InitialDataSpec),
        componentInstance = DataManagerComponent();

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(componentInstance.state.data).toEqual([]);
    // Since expect([]).toEqual({}) returns true we had to know for sure that
    // the data object is an Array
    expect(JSON.stringify(componentInstance.state.data)).toEqual('[]');
  });

  it("should override initial data with non-empty value", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var initialData = {name: 'Guest'},
        InitialDataSpec = _.extend({initialData: initialData}, DataManagerSpec),
        DataManagerComponent = React.createClass(InitialDataSpec),
        componentInstance = DataManagerComponent();

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(componentInstance.state.data).toEqual({name: 'Guest'});
  });

  it("should fetch data if a 'data' prop is set", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent({data: 'url?query=string'});

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(Fresh.mixins.DataManager
           .fetchDataFromServer.callCount).toEqual(1);
  });

  it("shouldn't fetch data if a 'data' prop isn't set", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent({});

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(Fresh.mixins.DataManager
           .fetchDataFromServer.callCount).toEqual(0);
  });

  it("should populate state.data with fetched data", function() {
    var DataManagerComponent = React.createClass(DataManagerSpec),
        componentInstance = DataManagerComponent({});

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    componentInstance.receiveDataFromServer({name: '<NAME>', age: 42});
    expect(componentInstance.state.data).toEqual({name: '<NAME>', age: 42});
  });

  it("should replace initial data after data is fetched", function() {
    // The fetching method should do nothing, we only care that it is called
    // before the components gets mounted
    spyOn(Fresh.mixins.DataManager, 'fetchDataFromServer');
    var initialData = {guest: true, name: 'Guest'},
        InitialDataSpec = _.extend({initialData: initialData}, DataManagerSpec),
        DataManagerComponent = React.createClass(InitialDataSpec),
        componentInstance = DataManagerComponent();

    // React Components need to be rendered to mount
    React.renderComponentToString(componentInstance);
    expect(componentInstance.state.data).toEqual({name: 'Guest', guest: true});
    componentInstance.receiveDataFromServer({name: '<NAME>', age: 42});
    expect(componentInstance.state.data).toEqual({name: '<NAME>', age: 42});
  });
});
{'fraction_non_alphanumeric': 0.11198512585812356, 'fraction_numerical': 0.002431350114416476, 'mean_word_length': 3.935074100211715, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4870349', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1808, 'n_words': 534}