arnaucube
arnaucube/raspberryGPIOhtmlserverPublic Self-hosted repository
raspberryGPIOhtmlserver/node_modules/formidable/test/legacy/simple/test-file.js
node_modules/formidable/test/legacy/simple/test-file.jsRaw
  1var common = require('../common');
  2var WriteStreamStub = GENTLY.stub('fs', 'WriteStream');
  3
  4var File = require(common.lib + '/file'),
  5    EventEmitter = require('events').EventEmitter,
  6    file,
  7    gently;
  8
  9function test(test) {
 10  gently = new Gently();
 11  file = new File();
 12  test();
 13  gently.verify(test.name);
 14}
 15
 16test(function constructor() {
 17  assert.ok(file instanceof EventEmitter);
 18  assert.strictEqual(file.size, 0);
 19  assert.strictEqual(file.path, null);
 20  assert.strictEqual(file.name, null);
 21  assert.strictEqual(file.type, null);
 22  assert.strictEqual(file.lastModifiedDate, null);
 23
 24  assert.strictEqual(file._writeStream, null);
 25
 26  (function testSetProperties() {
 27    var file2 = new File({foo: 'bar'});
 28    assert.equal(file2.foo, 'bar');
 29  })();
 30});
 31
 32test(function open() {
 33  var WRITE_STREAM;
 34  file.path = '/foo';
 35
 36  gently.expect(WriteStreamStub, 'new', function (path) {
 37    WRITE_STREAM = this;
 38    assert.strictEqual(path, file.path);
 39  });
 40
 41  file.open();
 42  assert.strictEqual(file._writeStream, WRITE_STREAM);
 43});
 44
 45test(function write() {
 46  var BUFFER = {length: 10},
 47      CB_STUB,
 48      CB = function() {
 49        CB_STUB.apply(this, arguments);
 50      };
 51
 52  file._writeStream = {};
 53
 54  gently.expect(file._writeStream, 'write', function (buffer, cb) {
 55    assert.strictEqual(buffer, BUFFER);
 56
 57    gently.expect(file, 'emit', function (event, bytesWritten) {
 58      assert.ok(file.lastModifiedDate instanceof Date);
 59      assert.equal(event, 'progress');
 60      assert.equal(bytesWritten, file.size);
 61    });
 62
 63    CB_STUB = gently.expect(function writeCb() {
 64      assert.equal(file.size, 10);
 65    });
 66
 67    cb();
 68
 69    gently.expect(file, 'emit', function (event, bytesWritten) {
 70      assert.equal(event, 'progress');
 71      assert.equal(bytesWritten, file.size);
 72    });
 73
 74    CB_STUB = gently.expect(function writeCb() {
 75      assert.equal(file.size, 20);
 76    });
 77
 78    cb();
 79  });
 80
 81  file.write(BUFFER, CB);
 82});
 83
 84test(function end() {
 85  var CB_STUB,
 86      CB = function() {
 87        CB_STUB.apply(this, arguments);
 88      };
 89
 90  file._writeStream = {};
 91
 92  gently.expect(file._writeStream, 'end', function (cb) {
 93    gently.expect(file, 'emit', function (event) {
 94      assert.equal(event, 'end');
 95    });
 96
 97    CB_STUB = gently.expect(function endCb() {
 98    });
 99
100    cb();
101  });
102
103  file.end(CB);
104});