react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / __tests__ / DOMPropertyOperations-test.js
81165 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";1213describe('DOMPropertyOperations', function() {14var DOMPropertyOperations;15var DOMProperty;1617var mocks;1819beforeEach(function() {20require('mock-modules').dumpCache();21var ReactDefaultInjection = require('ReactDefaultInjection');22ReactDefaultInjection.inject();2324DOMPropertyOperations = require('DOMPropertyOperations');25DOMProperty = require('DOMProperty');2627mocks = require('mocks');28});2930describe('createMarkupForProperty', function() {3132it('should create markup for simple properties', function() {33expect(DOMPropertyOperations.createMarkupForProperty(34'name',35'simple'36)).toBe('name="simple"');3738expect(DOMPropertyOperations.createMarkupForProperty(39'name',40false41)).toBe('name="false"');4243expect(DOMPropertyOperations.createMarkupForProperty(44'name',45null46)).toBe('');47});4849it('should work with the id attribute', function() {50expect(DOMPropertyOperations.createMarkupForProperty(51'id',52'simple'53)).toBe('id="simple"');54});5556it('should warn about incorrect casing', function() {57spyOn(console, 'warn');58expect(DOMPropertyOperations.createMarkupForProperty(59'tabindex',60'1'61)).toBe(null);62expect(console.warn.argsForCall.length).toBe(1);63expect(console.warn.argsForCall[0][0]).toContain('tabIndex');64});6566it('should warn about class', function() {67spyOn(console, 'warn');68expect(DOMPropertyOperations.createMarkupForProperty(69'class',70'muffins'71)).toBe(null);72expect(console.warn.argsForCall.length).toBe(1);73expect(console.warn.argsForCall[0][0]).toContain('className');74});7576it('should create markup for boolean properties', function() {77expect(DOMPropertyOperations.createMarkupForProperty(78'checked',79'simple'80)).toBe('checked');8182expect(DOMPropertyOperations.createMarkupForProperty(83'checked',84true85)).toBe('checked');8687expect(DOMPropertyOperations.createMarkupForProperty(88'checked',89false90)).toBe('');91});9293it('should create markup for booleanish properties', function() {94expect(DOMPropertyOperations.createMarkupForProperty(95'download',96'simple'97)).toBe('download="simple"');9899expect(DOMPropertyOperations.createMarkupForProperty(100'download',101true102)).toBe('download');103104expect(DOMPropertyOperations.createMarkupForProperty(105'download',106'true'107)).toBe('download="true"');108109expect(DOMPropertyOperations.createMarkupForProperty(110'download',111false112)).toBe('');113114expect(DOMPropertyOperations.createMarkupForProperty(115'download',116'false'117)).toBe('download="false"');118119expect(DOMPropertyOperations.createMarkupForProperty(120'download',121undefined122)).toBe('');123124expect(DOMPropertyOperations.createMarkupForProperty(125'download',126null127)).toBe('');128129expect(DOMPropertyOperations.createMarkupForProperty(130'download',1310132)).toBe('download="0"');133});134135it('should create markup for custom attributes', function() {136expect(DOMPropertyOperations.createMarkupForProperty(137'aria-label',138'simple'139)).toBe('aria-label="simple"');140141expect(DOMPropertyOperations.createMarkupForProperty(142'aria-label',143false144)).toBe('aria-label="false"');145146expect(DOMPropertyOperations.createMarkupForProperty(147'aria-label',148null149)).toBe('');150});151152it('should create markup for numeric properties', function() {153expect(DOMPropertyOperations.createMarkupForProperty(154'start',1555156)).toBe('start="5"');157158expect(DOMPropertyOperations.createMarkupForProperty(159'start',1600161)).toBe('start="0"');162163expect(DOMPropertyOperations.createMarkupForProperty(164'size',1650166)).toBe('');167168expect(DOMPropertyOperations.createMarkupForProperty(169'size',1701171)).toBe('size="1"');172});173174});175176describe('setValueForProperty', function() {177var stubNode;178179beforeEach(function() {180stubNode = document.createElement('div');181});182183it('should set values as properties by default', function() {184DOMPropertyOperations.setValueForProperty(stubNode, 'title', 'Tip!');185expect(stubNode.title).toBe('Tip!');186});187188it('should set values as attributes if necessary', function() {189DOMPropertyOperations.setValueForProperty(stubNode, 'role', '#');190expect(stubNode.getAttribute('role')).toBe('#');191expect(stubNode.role).toBeUndefined();192});193194it('should convert attribute values to string first', function() {195// Browsers default to this behavior, but some test environments do not.196// This ensures that we have consistent behavior.197var obj = {toString: function() { return '<html>'; }};198DOMPropertyOperations.setValueForProperty(stubNode, 'role', obj);199expect(stubNode.getAttribute('role')).toBe('<html>');200});201202it('should remove for falsey boolean properties', function() {203DOMPropertyOperations.setValueForProperty(204stubNode,205'allowFullScreen',206false207);208expect(stubNode.hasAttribute('allowFullScreen')).toBe(false);209});210211it('should remove when setting custom attr to null', function() {212DOMPropertyOperations.setValueForProperty(213stubNode,214'data-foo',215'bar'216);217expect(stubNode.hasAttribute('data-foo')).toBe(true);218DOMPropertyOperations.setValueForProperty(219stubNode,220'data-foo',221null222);223expect(stubNode.hasAttribute('data-foo')).toBe(false);224});225226it('should use mutation method where applicable', function() {227var foobarSetter = mocks.getMockFunction();228// inject foobar DOM property229DOMProperty.injection.injectDOMPropertyConfig({230Properties: {foobar: null},231DOMMutationMethods: {232foobar: foobarSetter233}234});235236DOMPropertyOperations.setValueForProperty(237stubNode,238'foobar',239'cows say moo'240);241242expect(foobarSetter.mock.calls.length).toBe(1);243expect(foobarSetter.mock.calls[0][0]).toBe(stubNode);244expect(foobarSetter.mock.calls[0][1]).toBe('cows say moo');245});246247it('should set className to empty string instead of null', function() {248DOMPropertyOperations.setValueForProperty(249stubNode,250'className',251'selected'252);253expect(stubNode.className).toBe('selected');254255DOMPropertyOperations.setValueForProperty(256stubNode,257'className',258null259);260// className should be '', not 'null' or null (which becomes 'null' in261// some browsers)262expect(stubNode.className).toBe('');263});264265it('should remove property properly even with different name', function() {266// Suppose 'foobar' is a property that corresponds to the underlying267// 'className' property:268DOMProperty.injection.injectDOMPropertyConfig({269Properties: {foobar: DOMProperty.injection.MUST_USE_PROPERTY},270DOMPropertyNames: {271foobar: 'className'272}273});274275DOMPropertyOperations.setValueForProperty(276stubNode,277'foobar',278'selected'279);280expect(stubNode.className).toBe('selected');281282DOMPropertyOperations.setValueForProperty(283stubNode,284'foobar',285null286);287// className should be '', not 'null' or null (which becomes 'null' in288// some browsers)289expect(stubNode.className).toBe('');290});291292});293294describe('injectDOMPropertyConfig', function() {295it('should support custom attributes', function() {296// foobar does not exist yet297expect(DOMPropertyOperations.createMarkupForProperty(298'foobar',299'simple'300)).toBe(null);301302// foo-* does not exist yet303expect(DOMPropertyOperations.createMarkupForProperty(304'foo-xyz',305'simple'306)).toBe(null);307308// inject foobar DOM property309DOMProperty.injection.injectDOMPropertyConfig({310isCustomAttribute: function(name) {311return name.indexOf('foo-') === 0;312},313Properties: {foobar: null}314});315316// Ensure old attributes still work317expect(DOMPropertyOperations.createMarkupForProperty(318'name',319'simple'320)).toBe('name="simple"');321expect(DOMPropertyOperations.createMarkupForProperty(322'data-name',323'simple'324)).toBe('data-name="simple"');325326// foobar should work327expect(DOMPropertyOperations.createMarkupForProperty(328'foobar',329'simple'330)).toBe('foobar="simple"');331332// foo-* should work333expect(DOMPropertyOperations.createMarkupForProperty(334'foo-xyz',335'simple'336)).toBe('foo-xyz="simple"');337338// It should complain about double injections.339expect(function() {340DOMProperty.injection.injectDOMPropertyConfig(341{Properties: {foobar: null}}342);343}).toThrow();344});345});346});347348349