2015-02-12 19:51:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
jest.autoMockOff();
|
2015-02-12 19:51:52 +00:00
|
|
|
|
|
|
|
describe('React documentation parser', function() {
|
|
|
|
var ReactDocumentationParser;
|
|
|
|
var parser;
|
2015-02-19 00:39:28 +00:00
|
|
|
var recast;
|
2015-02-12 19:51:52 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2015-02-19 00:39:28 +00:00
|
|
|
recast = require('recast');
|
2015-02-12 19:51:52 +00:00
|
|
|
ReactDocumentationParser = require('../ReactDocumentationParser');
|
|
|
|
parser = new ReactDocumentationParser();
|
|
|
|
});
|
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
function pathFromSource(source) {
|
|
|
|
return new recast.types.NodePath(
|
|
|
|
recast.parse(source).program.body[0].expression
|
|
|
|
);
|
|
|
|
}
|
2015-02-12 19:51:52 +00:00
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
describe('parseSource', function() {
|
2015-02-12 19:51:52 +00:00
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
it('allows custom component definition resolvers', function() {
|
|
|
|
var path = pathFromSource('({foo: "bar"})');
|
|
|
|
var resolver = jest.genMockFunction().mockReturnValue(path);
|
|
|
|
var handler = jest.genMockFunction();
|
|
|
|
parser.addHandler(handler);
|
|
|
|
parser.parseSource('', resolver);
|
2015-02-12 19:51:52 +00:00
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
expect(resolver).toBeCalled();
|
|
|
|
expect(handler.mock.calls[0][1]).toBe(path);
|
|
|
|
});
|
2015-02-12 19:51:52 +00:00
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
it('errors if component definition is not found', function() {
|
|
|
|
var handler = jest.genMockFunction();
|
|
|
|
expect(function() {
|
|
|
|
parser.parseSource('', handler);
|
|
|
|
}).toThrow(ReactDocumentationParser.ERROR_MISSING_DEFINITION);
|
|
|
|
expect(handler).toBeCalled();
|
2015-02-12 19:51:52 +00:00
|
|
|
|
2015-02-19 00:39:28 +00:00
|
|
|
handler = jest.genMockFunction().mockReturnValue([]);
|
|
|
|
expect(function() {
|
|
|
|
parser.parseSource('', handler);
|
|
|
|
}).toThrow(ReactDocumentationParser.ERROR_MISSING_DEFINITION);
|
|
|
|
expect(handler).toBeCalled();
|
|
|
|
});
|
2015-02-12 19:51:52 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|