|
| 1 | +import React from 'react'; |
| 2 | +import chai, {expect} from 'chai'; |
| 3 | +import {shallow, mount} from 'enzyme'; |
| 4 | +import Audio from '../src/components/Audio'; |
| 5 | +import Transformation from '../src/components/Transformation'; |
| 6 | + |
| 7 | +chai.use(require('chai-string')); |
| 8 | + |
| 9 | +describe('Audio', () => { |
| 10 | + it("should include child transformation for a single source type", function () { |
| 11 | + let tag = shallow( |
| 12 | + <Audio cloudName='demo' |
| 13 | + sourceTypes={'wav'} |
| 14 | + publicId='dog' |
| 15 | + sourceTransformation={{ |
| 16 | + wav: {duration: 3} |
| 17 | + }}> |
| 18 | + <Transformation quality='70'/> |
| 19 | + </Audio>); |
| 20 | + expect(tag.props().src).to.endWith('/q_70/du_3/dog.wav'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should support startOffset parameter', function () { |
| 24 | + let tag = shallow( |
| 25 | + <Audio cloudName="demo" sourceTypes={'wav'} publicId="dog"> |
| 26 | + <Transformation startOffset="auto"/> |
| 27 | + </Audio>); |
| 28 | + expect(tag.props().src).to.endWith('/so_auto/dog.wav'); |
| 29 | + tag = shallow( |
| 30 | + <Audio cloudName="demo" sourceTypes={'wav'} publicId="dog"> |
| 31 | + <Transformation startOffset="2"/> |
| 32 | + </Audio>); |
| 33 | + expect(tag.props().src).to.endWith('/so_2/dog.wav'); |
| 34 | + tag = shallow( |
| 35 | + <Audio cloudName="demo" sourceTypes={'wav'} publicId="dog"> |
| 36 | + <Transformation startOffset="2.34"/> |
| 37 | + </Audio>); |
| 38 | + expect(tag.props().src).to.endWith('/so_2.34/dog.wav'); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should include child transformation for multiple source types", function () { |
| 42 | + let tag = shallow( |
| 43 | + <Audio cloudName='demo' |
| 44 | + sourceTypes={['wav', 'mp3']} |
| 45 | + publicId='dog' |
| 46 | + sourceTransformation={{ |
| 47 | + wav: {effect: "volume:30"}, |
| 48 | + mp3: {effect: "volume:45"} |
| 49 | + }}> |
| 50 | + <Transformation duration="2"/> |
| 51 | + </Audio>); |
| 52 | + expect(tag.find('[type="audio/wav"]').props().src).to.endWith('/du_2/e_volume:30/dog.wav'); |
| 53 | + expect(tag.find('[type="audio/mp3"]').props().src).to.endWith('/du_2/e_volume:45/dog.mp3'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should support inner text', function () { |
| 57 | + let tag = shallow( |
| 58 | + <Audio cloudName='demo' publicId='dog'> |
| 59 | + Your browser does not support the audio tag. |
| 60 | + </Audio> |
| 61 | + ); |
| 62 | + expect(tag.type()).to.equal("audio"); |
| 63 | + }); |
| 64 | + |
| 65 | + it('Should support forwarding innerRef to underlying audio element', function () { |
| 66 | + let myRef = React.createRef(); |
| 67 | + |
| 68 | + let tag = mount( |
| 69 | + <Audio |
| 70 | + innerRef={myRef} |
| 71 | + cloudName='demo' |
| 72 | + sourceTypes='ogg' |
| 73 | + publicId='dog' |
| 74 | + sourceTransformation={{ |
| 75 | + ogg: {duration: 2} |
| 76 | + }} |
| 77 | + /> |
| 78 | + ); |
| 79 | + |
| 80 | + const audio = myRef.current; |
| 81 | + |
| 82 | + expect(tag.find('audio').prop('src')).to.endWith('/du_2/dog.ogg'); |
| 83 | + expect(audio.src).to.endWith('/du_2/dog.ogg'); |
| 84 | + ['play', 'pause', 'canPlayType', 'addTextTrack'].forEach(func => expect(audio[func]).to.be.a('function')); |
| 85 | + }); |
| 86 | + |
| 87 | + it('Should not set a poster attribute', function () { |
| 88 | + let tag = shallow( |
| 89 | + <Audio cloudName='demo' publicId='dog'/> |
| 90 | + ); |
| 91 | + |
| 92 | + expect(tag.props().poster).to.equal(undefined); |
| 93 | + }); |
| 94 | + |
| 95 | + it('Should pass camelCase attributes to Audio component', function () { |
| 96 | + const tag = shallow( |
| 97 | + <Audio playsInline autoPlay cloudName='demo' publicId='dog'/> |
| 98 | + ); |
| 99 | + |
| 100 | + const {autoPlay, auto_play} = tag.props(); |
| 101 | + |
| 102 | + expect(autoPlay).to.equal(true); |
| 103 | + |
| 104 | + expect(auto_play).to.equal(undefined); |
| 105 | + }); |
| 106 | + it('Should pass camelCase attributes to inner audio element', function () { |
| 107 | + let tag = mount( |
| 108 | + <Audio autoPlay cloudName='demo' publicId='dog'/> |
| 109 | + ); |
| 110 | + |
| 111 | + const audio = tag.find('audio'); |
| 112 | + expect(audio.prop('autoPlay')).to.equal(true); |
| 113 | + |
| 114 | + expect(audio.prop('plays_inline')).to.equal(undefined); |
| 115 | + expect(audio.prop('auto_play')).to.equal(undefined); |
| 116 | + }); |
| 117 | + it("should generate default source tags", function () { |
| 118 | + let tag = shallow( |
| 119 | + <Audio cloudName='demo' |
| 120 | + publicId='dog' |
| 121 | + sourceTransformation={{ |
| 122 | + aac: {duration: 1}, |
| 123 | + mp3: {duration: 2}, |
| 124 | + ogg: {duration: 3}, |
| 125 | + }}> |
| 126 | + <Transformation quality='70'/> |
| 127 | + </Audio>); |
| 128 | + expect(tag.find('[type="audio/aac"]').props().src).to.endWith('/du_1/dog.aac'); |
| 129 | + expect(tag.find('[type="audio/mp3"]').props().src).to.endWith('/du_2/dog.mp3'); |
| 130 | + expect(tag.find('[type="audio/ogg"]').props().src).to.endWith('/du_3/dog.ogg'); |
| 131 | + }); |
| 132 | +}); |
0 commit comments