| 1 | /*global Autolinker */ |
| 2 | /** |
| 3 | * @class Autolinker.match.MatchChecker |
| 4 | * @singleton |
| 5 | * |
| 6 | * A testing utility used to easily make assertions about |
| 7 | * {@link Autolinker.match.Match} objects. |
| 8 | */ |
| 9 | Autolinker.match.MatchChecker = { |
| 10 | |
| 11 | /** |
| 12 | * Expects an {@link Autolinker.match.Email Email} match. |
| 13 | * |
| 14 | * @param {Autolinker.match.Email} match The Match object to check. |
| 15 | * @param {String} email The email address to expect. |
| 16 | * @param {Number} offset The offset for the match in the original string to |
| 17 | * expect. |
| 18 | */ |
| 19 | expectEmailMatch : function( match, email, offset ) { |
| 20 | this.expectMatchType( match, 'Email' ); |
| 21 | |
| 22 | expect( match.getEmail() ).toBe( email ); |
| 23 | expect( match.getOffset() ).toBe( offset ); |
| 24 | }, |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Expects a {@link Autolinker.match.Hashtag Hashtag} match. |
| 29 | * |
| 30 | * @param {Autolinker.match.Hashtag} match The Match object to check. |
| 31 | * @param {String} serviceName The service name to expect of where to direct |
| 32 | * clicks to the hashtag to. Ex: 'facebook', 'twitter'. |
| 33 | * @param {String} hashtag The hashtag to expect, without the prefixed '#' |
| 34 | * character. |
| 35 | * @param {Number} offset The offset for the match in the original string to |
| 36 | * expect. |
| 37 | */ |
| 38 | expectHashtagMatch : function( match, serviceName, hashtag, offset ) { |
| 39 | this.expectMatchType( match, 'Hashtag' ); |
| 40 | |
| 41 | expect( match.getServiceName() ).toBe( serviceName ); |
| 42 | expect( match.getHashtag() ).toBe( hashtag ); |
| 43 | expect( match.getOffset() ).toBe( offset ); |
| 44 | }, |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Expects a {@link Autolinker.match.Phone Phone} match. |
| 49 | * |
| 50 | * @param {Autolinker.match.Phone} match The Match object to check. |
| 51 | * @param {String} number The phone number to expect, without any delimiter |
| 52 | * characters, and without a prefixed '+' character. |
| 53 | * @param {Number} offset The offset for the match in the original string to |
| 54 | * expect. |
| 55 | */ |
| 56 | expectPhoneMatch : function( match, number, offset ) { |
| 57 | this.expectMatchType( match, 'Phone' ); |
| 58 | |
| 59 | expect( match.getNumber() ).toBe( number ); |
| 60 | expect( match.getOffset() ).toBe( offset ); |
| 61 | }, |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Expects a {@link Autolinker.match.Mention Mention} match. |
| 66 | * |
| 67 | * @param {Autolinker.match.Mention} match The Match object to check. |
| 68 | * @param {String} serviceName The service name to expect of where to direct |
| 69 | * clicks to the mention to. Ex: 'twitter', 'instagram'. |
| 70 | * @param {String} mention The mention to expect, without the |
| 71 | * prefixed '@' character. |
| 72 | * @param {Number} offset The offset for the match in the original string to |
| 73 | * expect. |
| 74 | */ |
| 75 | expectMentionMatch : function( match, serviceName, mention, offset ) { |
| 76 | this.expectMatchType( match, 'Mention' ); |
| 77 | |
| 78 | expect( match.getServiceName() ).toBe( serviceName ); |
| 79 | expect( match.getMention() ).toBe( mention ); |
| 80 | expect( match.getOffset() ).toBe( offset ); |
| 81 | }, |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Expects a {@link Autolinker.match.Url Url} match. |
| 86 | * |
| 87 | * @param {Autolinker.match.Url} match The Match object to check. |
| 88 | * @param {String} url The URL to expect, with the URI scheme prepended. |
| 89 | * @param {Number} offset The offset for the match in the original string to |
| 90 | * expect. |
| 91 | */ |
| 92 | expectUrlMatch : function( match, url, offset ) { |
| 93 | this.expectMatchType( match, 'Url' ); |
| 94 | |
| 95 | expect( match.getUrl() ).toBe( url ); |
| 96 | expect( match.getOffset() ).toBe( offset ); |
| 97 | }, |
| 98 | |
| 99 | |
| 100 | // --------------------------------------- |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Private utility method used to check the type of the `match` object |
| 105 | * provided, and throws if it does not match the provided `typeName`. |
| 106 | * |
| 107 | * @param {Autolinker.match.Match} match The Match object to check against |
| 108 | * the provided `typeName`. |
| 109 | * @param {String} typeName The name of the Match subclass. Ex: 'Email', |
| 110 | * 'Twitter', 'Url', etc. |
| 111 | * @throws {Error} If the `match` is not an instance of the `typeName`. |
| 112 | */ |
| 113 | expectMatchType : function( match, typeName ) { |
| 114 | if( !( match instanceof Autolinker.match[ typeName ] ) ) { |
| 115 | throw new Error( 'Expected an Autolinker.match.' + typeName + ' match object' ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | }; |