react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / event / __tests__ / EventPluginRegistry-test.js
81155 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @emails react-core9*/1011"use strict";1213var assign = require('Object.assign');1415describe('EventPluginRegistry', function() {16var EventPluginRegistry;17var createPlugin;1819beforeEach(function() {20EventPluginRegistry = require('EventPluginRegistry');21EventPluginRegistry._resetEventPlugins();2223createPlugin = function(properties) {24return assign({extractEvents: function() {}}, properties);25};26});2728it('should be able to inject ordering before plugins', function() {29var OnePlugin = createPlugin();30var TwoPlugin = createPlugin();31var ThreePlugin = createPlugin();3233EventPluginRegistry.injectEventPluginOrder(['one', 'two', 'three']);34EventPluginRegistry.injectEventPluginsByName({35one: OnePlugin,36two: TwoPlugin37});38EventPluginRegistry.injectEventPluginsByName({39three: ThreePlugin40});4142expect(EventPluginRegistry.plugins.length).toBe(3);43expect(EventPluginRegistry.plugins[0]).toBe(OnePlugin);44expect(EventPluginRegistry.plugins[1]).toBe(TwoPlugin);45expect(EventPluginRegistry.plugins[2]).toBe(ThreePlugin);46});4748it('should be able to inject plugins before and after ordering', function() {49var OnePlugin = createPlugin();50var TwoPlugin = createPlugin();51var ThreePlugin = createPlugin();5253EventPluginRegistry.injectEventPluginsByName({54one: OnePlugin,55two: TwoPlugin56});57EventPluginRegistry.injectEventPluginOrder(['one', 'two', 'three']);58EventPluginRegistry.injectEventPluginsByName({59three: ThreePlugin60});6162expect(EventPluginRegistry.plugins.length).toBe(3);63expect(EventPluginRegistry.plugins[0]).toBe(OnePlugin);64expect(EventPluginRegistry.plugins[1]).toBe(TwoPlugin);65expect(EventPluginRegistry.plugins[2]).toBe(ThreePlugin);66});6768it('should be able to inject repeated plugins and out-of-order', function() {69var OnePlugin = createPlugin();70var TwoPlugin = createPlugin();71var ThreePlugin = createPlugin();7273EventPluginRegistry.injectEventPluginsByName({74one: OnePlugin,75three: ThreePlugin76});77EventPluginRegistry.injectEventPluginOrder(['one', 'two', 'three']);78EventPluginRegistry.injectEventPluginsByName({79two: TwoPlugin,80three: ThreePlugin81});8283expect(EventPluginRegistry.plugins.length).toBe(3);84expect(EventPluginRegistry.plugins[0]).toBe(OnePlugin);85expect(EventPluginRegistry.plugins[1]).toBe(TwoPlugin);86expect(EventPluginRegistry.plugins[2]).toBe(ThreePlugin);87});8889it('should throw if plugin does not implement `extractEvents`', function() {90var BadPlugin = {};9192EventPluginRegistry.injectEventPluginOrder(['bad']);9394expect(function() {95EventPluginRegistry.injectEventPluginsByName({96bad: BadPlugin97});98}).toThrow(99'Invariant Violation: EventPluginRegistry: Event plugins must ' +100'implement an `extractEvents` method, but `bad` does not.'101);102});103104it('should throw if plugin does not exist in ordering', function() {105var OnePlugin = createPlugin();106var RandomPlugin = createPlugin();107108EventPluginRegistry.injectEventPluginOrder(['one']);109110expect(function() {111EventPluginRegistry.injectEventPluginsByName({112one: OnePlugin,113random: RandomPlugin114});115}).toThrow(116'Invariant Violation: EventPluginRegistry: Cannot inject event plugins ' +117'that do not exist in the plugin ordering, `random`.'118);119});120121it('should throw if ordering is injected more than once', function() {122var pluginOrdering = [];123124EventPluginRegistry.injectEventPluginOrder(pluginOrdering);125126expect(function() {127EventPluginRegistry.injectEventPluginOrder(pluginOrdering);128}).toThrow(129'Invariant Violation: EventPluginRegistry: Cannot inject event plugin ' +130'ordering more than once. You are likely trying to load more than one ' +131'copy of React.'132);133});134135it('should throw if different plugins injected using same name', function() {136var OnePlugin = createPlugin();137var TwoPlugin = createPlugin();138139EventPluginRegistry.injectEventPluginsByName({same: OnePlugin});140141expect(function() {142EventPluginRegistry.injectEventPluginsByName({same: TwoPlugin});143}).toThrow(144'Invariant Violation: EventPluginRegistry: Cannot inject two different ' +145'event plugins using the same name, `same`.'146);147});148149it('should publish registration names of injected plugins', function() {150var OnePlugin = createPlugin({151eventTypes: {152click: {registrationName: 'onClick'},153focus: {registrationName: 'onFocus'}154}155});156var TwoPlugin = createPlugin({157eventTypes: {158magic: {159phasedRegistrationNames: {160bubbled: 'onMagicBubble',161captured: 'onMagicCapture'162}163}164}165});166167EventPluginRegistry.injectEventPluginsByName({one: OnePlugin});168EventPluginRegistry.injectEventPluginOrder(['one', 'two']);169170expect(Object.keys(EventPluginRegistry.registrationNameModules).length).toBe(2);171expect(EventPluginRegistry.registrationNameModules.onClick).toBe(OnePlugin);172expect(EventPluginRegistry.registrationNameModules.onFocus).toBe(OnePlugin);173174EventPluginRegistry.injectEventPluginsByName({two: TwoPlugin});175176expect(Object.keys(EventPluginRegistry.registrationNameModules).length).toBe(4);177expect(EventPluginRegistry.registrationNameModules.onMagicBubble).toBe(TwoPlugin);178expect(179EventPluginRegistry.registrationNameModules.onMagicCapture180).toBe(TwoPlugin);181});182183it('should throw if multiple registration names collide', function() {184var OnePlugin = createPlugin({185eventTypes: {186photoCapture: {registrationName: 'onPhotoCapture'}187}188});189var TwoPlugin = createPlugin({190eventTypes: {191photo: {192phasedRegistrationNames: {193bubbled: 'onPhotoBubble',194captured: 'onPhotoCapture'195}196}197}198});199200EventPluginRegistry.injectEventPluginsByName({201one: OnePlugin,202two: TwoPlugin203});204205expect(function() {206EventPluginRegistry.injectEventPluginOrder(['one', 'two']);207}).toThrow(208'Invariant Violation: EventPluginHub: More than one plugin attempted ' +209'to publish the same registration name, `onPhotoCapture`.'210);211});212213it('should throw if an invalid event is published', function() {214var OnePlugin = createPlugin({215eventTypes: {216badEvent: {/* missing configuration */}217}218});219220EventPluginRegistry.injectEventPluginsByName({one: OnePlugin});221222expect(function() {223EventPluginRegistry.injectEventPluginOrder(['one']);224}).toThrow(225'Invariant Violation: EventPluginRegistry: Failed to publish event ' +226'`badEvent` for plugin `one`.'227);228});229230it('should be able to get the plugin from synthetic events', function() {231var clickDispatchConfig = {232registrationName: 'onClick'233};234var magicDispatchConfig = {235phasedRegistrationNames: {236bubbled: 'onMagicBubble',237captured: 'onMagicCapture'238}239};240241var OnePlugin = createPlugin({242eventTypes: {243click: clickDispatchConfig,244magic: magicDispatchConfig245}246});247248var clickEvent = {dispatchConfig: clickDispatchConfig};249var magicEvent = {dispatchConfig: magicDispatchConfig};250251expect(EventPluginRegistry.getPluginModuleForEvent(clickEvent)).toBe(null);252expect(EventPluginRegistry.getPluginModuleForEvent(magicEvent)).toBe(null);253254EventPluginRegistry.injectEventPluginsByName({one: OnePlugin});255EventPluginRegistry.injectEventPluginOrder(['one']);256257expect(258EventPluginRegistry.getPluginModuleForEvent(clickEvent)259).toBe(OnePlugin);260expect(261EventPluginRegistry.getPluginModuleForEvent(magicEvent)262).toBe(OnePlugin);263});264265});266267268