react / wstein / node_modules / jest-cli / node_modules / node-haste / __tests__ / MapUpdateTask-test.js
80668 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*15* @emails [email protected] [email protected]16*/1718describe("MapUpdateTask", function() {19var MapUpdateTask = require('../lib/MapUpdateTask');20var Resource = require('../lib/resource/Resource');21var ResourceLoader = require('../lib/loader/ResourceLoader');22var ResourceMap = require('../lib/ResourceMap');23var ProjectConfiguration = require('../lib/resource/ProjectConfiguration');24var ProjectConfigurationLoader =25require('../lib/loader/ProjectConfigurationLoader');26var MessageList = require('../lib/MessageList');2728var waitsForCallback = require('../lib/test_helpers/waitsForCallback');29var node_path = require('path');3031function expectChanges(changed, expected) {32expect(changed.length).toBe(expected.length);33var pathMap = {};34changed.map(function(record) {35pathMap[record.path] = record;36});37expected.forEach(function(pair) {38expect(pathMap[pair[1]]).toBeDefined('Change exists ' + pair[1]);39if (pair[0] === 'added') {40expect(pathMap[pair[1]].oldResource).toBe(null);41expect(pathMap[pair[1]].newPath).toBe(pair[1]);42} else if (pair[0] === 'changed') {43expect(pathMap[pair[1]].oldResource.path).toBe(pair[1]);44expect(pathMap[pair[1]].newPath).toBe(pair[1]);45} else if (pair[0] === 'removed') {46expect(pathMap[pair[1]].oldResource.path).toBe(pair[1]);47expect(pathMap[pair[1]].newPath).toBe(null);48}49});50}5152function addMtime(mtime, resource) {53resource.mtime = mtime;54return resource;55}5657it("should find changed files", function() {58var files = [59['sub/added.js', 1300000000000],60['changed.js', 1300000000000],61['unmodified.js', 1300000000000]62];63var map = new ResourceMap([64addMtime(1200000000000, new Resource('sub/removed.js')),65addMtime(1200000000000, new Resource('changed.js')),66addMtime(1300000000000, new Resource('unmodified.js'))67]);68var task = new MapUpdateTask(69files,70[],71map);72var changed;7374runs(function() {75task.on('changed', function(tmp) {76changed = tmp;77});78task.run();79});8081waitsFor(function() {82return changed;83}, 300);8485runs(function() {86expectChanges(changed, [87['added', node_path.join('sub','added.js')],88['removed', node_path.join('sub','removed.js')],89['changed', node_path.join('changed.js')]90]);91});92});9394it('should find changes on package.json deletion', function() {95var files = [96['p1/a/1.js', 1300000000000],97['p1/b/2.js', 1300000000000]98];99var map = new ResourceMap([100addMtime(1300000000000, new Resource('p1/a/1.js')),101addMtime(1300000000000, new Resource('p1/b/2.js')),102addMtime(1300000000000, new ProjectConfiguration('p1/package.json', {}))103]);104var task = new MapUpdateTask(105files,106[],107map);108var changed;109110runs(function() {111task.on('changed', function(tmp) {112changed = tmp;113});114task.run();115});116117waitsFor(function() {118return changed;119}, 300);120121runs(function() {122expectChanges(changed, [123['changed', node_path.join('p1','a','1.js')],124['changed', node_path.join('p1','b','2.js')],125['removed', node_path.join('p1','package.json')]126]);127});128});129130it('should find changes on package.json deletion + haste dirs', function() {131var files = [132['p1/a/1.js', 1300000000000],133['p1/b/2.js', 1300000000000]134];135var map = new ResourceMap([136addMtime(1300000000000, new Resource('p1/a/1.js')),137addMtime(1300000000000, new Resource('p1/b/2.js')),138addMtime(1300000000000, new ProjectConfiguration('p1/package.json', {139haste: { roots: ['a'] }140}))141]);142var task = new MapUpdateTask(files, [], map);143var changed;144145runs(function() {146task.on('changed', function(tmp) {147changed = tmp;148});149task.run();150});151152waitsFor(function() {153return changed;154}, 300);155156runs(function() {157expectChanges(changed, [158['changed', node_path.join('p1','a','1.js')],159['removed', node_path.join('p1','package.json')]160]);161});162});163164it('should find changes on package.json addition', function() {165var files = [166['p1/a/1.js', 1300000000000],167['p1/b/2.js', 1300000000000],168['p1/package.json', 1300000000000]169];170var map = new ResourceMap([171addMtime(1300000000000, new Resource('p1/a/1.js')),172addMtime(1300000000000, new Resource('p1/b/2.js'))173]);174var configurationLoader = new ProjectConfigurationLoader();175spyOn(configurationLoader, 'loadFromPath')176.andCallFake(function(path, configuration, callback) {177callback(178new MessageList(),179new ProjectConfiguration('p1/package.json', {}));180});181182var task = new MapUpdateTask(files, [configurationLoader], map);183var changed;184185runs(function() {186task.on('changed', function(tmp) {187changed = tmp;188});189task.run();190});191192waitsFor(function() {193return changed;194}, 300);195196runs(function() {197expectChanges(changed, [198['changed', node_path.join('p1','a','1.js')],199['changed', node_path.join('p1','b','2.js')],200['added', node_path.join('p1','package.json')]201]);202});203});204205it('should find changes on package.json change', function() {206var files = [207['p1/a/1.js', 1300000000000],208['p1/b/2.js', 1300000000000],209['p1/package.json', 1300000000000]210];211var map = new ResourceMap([212addMtime(1300000000000, new Resource('p1/a/1.js')),213addMtime(1300000000000, new Resource('p1/b/2.js')),214addMtime(1200000000000, new ProjectConfiguration('p1/package.json', {215haste: { roots: ['a'] }216}))217]);218var configurationLoader = new ProjectConfigurationLoader();219spyOn(configurationLoader, 'loadFromPath')220.andCallFake(function(path, configuration, callback) {221expect(path).toBe(node_path.join('p1','package.json'));222callback(223new MessageList(),224new ProjectConfiguration('p1/package.json', {}));225});226227var task = new MapUpdateTask(files, [configurationLoader], map);228var changed;229230runs(function() {231task.on('changed', function(tmp) {232changed = tmp;233});234task.run();235});236237waitsFor(function() {238return changed;239}, 300);240241runs(function() {242expectChanges(changed, [243['changed', node_path.join('p1','a','1.js')],244['changed', node_path.join('p1','b','2.js')],245['changed', node_path.join('p1','package.json')]246]);247});248});249250it('should find changes on package.json change + haste dirs', function() {251var files = [252['p1/a/1.js', 1300000000000],253['p1/b/2.js', 1300000000000],254['p1/package.json', 1300000000000]255];256var map = new ResourceMap([257addMtime(1300000000000, new Resource('p1/a/1.js')),258addMtime(1300000000000, new Resource('p1/b/2.js')),259addMtime(1200000000000, new ProjectConfiguration('p1/package.json', {260haste: { roots: ['a'] }261}))262]);263var configurationLoader = new ProjectConfigurationLoader();264spyOn(configurationLoader, 'loadFromPath')265.andCallFake(function(path, configuration, callback) {266callback(267new MessageList(),268new ProjectConfiguration('p1/package.json', {269haste: { roots: ['a'] }270}));271});272273var task = new MapUpdateTask(files, [configurationLoader], map);274var changed;275276runs(function() {277task.on('changed', function(tmp) {278changed = tmp;279});280task.run();281});282283waitsFor(function() {284return changed;285}, 300);286287runs(function() {288expectChanges(changed, [289['changed', node_path.join('p1','a','1.js')],290['changed', node_path.join('p1','package.json')]291]);292});293});294295it('should load resource when changed', function() {296var finder = [297['sub/added.js', 1300000000000]298];299var map = new ResourceMap([]);300var loader = new ResourceLoader();301var task = new MapUpdateTask(finder, [loader], map);302spyOn(loader, 'loadFromPath')303.andCallFake(function(path, configuration, callback) {304expect(path).toBe(node_path.join('sub','added.js'));305callback(new MessageList(), new Resource('sub/added.js'));306});307308waitsForCallback(309function(callback) {310task.on('complete', callback);311task.run();312},313function() {}314);315});316317it('should not load deleted resource', function() {318var files = [];319var old = addMtime(1200000000000, new Resource('sub/deleted.js'));320var map = new ResourceMap([old]);321var loader = new ResourceLoader();322var task = new MapUpdateTask(files, [loader], map);323spyOn(loader, 'loadFromPath');324325waitsForCallback(326function(callback) {327task.on('complete', callback).run();328},329function() {330expect(loader.loadFromPath).not.toHaveBeenCalled();331}332);333});334335it('should aggregate messages from loaders', function() {336var files = [337['sub/new1.js', 1300000000000],338['sub/new2.js', 1300000000000]339];340var map = new ResourceMap([]);341var loader = new ResourceLoader();342var task = new MapUpdateTask(files, [loader], map);343spyOn(loader, 'loadFromPath')344.andCallFake(function(path, configuration, callback) {345var messages = new MessageList();346messages.addError(path, 'foo', 'bar');347callback(messages, new Resource(path));348});349350waitsForCallback(351function(callback) {352task.on('complete', callback).run();353},354function() {355expect(task.messages.length).toBe(2);356}357);358});359360it('should aggregate messages from postProcess', function() {361var files = [362['sub/new1.js', 1300000000000],363['sub/new2.js', 1300000000000]364];365var map = new ResourceMap([]);366var loader = new ResourceLoader();367var task = new MapUpdateTask(files, [loader], map);368spyOn(loader, 'loadFromPath')369.andCallFake(function(path, configuration, callback) {370var messages = new MessageList();371process.nextTick(function() {372callback(messages, new Resource(path));373});374});375376spyOn(loader, 'postProcess')377.andCallFake(function(map, resources, callback) {378expect(resources.length).toBe(2);379expect(resources[0]).toEqual(jasmine.any(Resource));380var messages = new MessageList();381resources.forEach(function(resource) {382messages.addError(resource.path, 'foo', 'bar');383});384callback(messages);385});386387waitsForCallback(388function(callback) {389task.on('complete', callback).run();390},391function() {392expect(task.messages.length).toBe(2);393}394);395});396397});398399400