|
| 1 | +const { expect, setURL } = require('./tests_common'); |
| 2 | +const Url = require('../url'); |
| 3 | + |
| 4 | +describe('Url', () => { |
| 5 | + const website_url = Url.websiteUrl(); |
| 6 | + const language = 'en'; |
| 7 | + const query_string = 'market=forex&duration_amount=5&no_value='; |
| 8 | + const params_obj = { market: 'forex', duration_amount: '5', no_value: '' }; |
| 9 | + const url_no_qs = `${website_url}${language}/trading.html`; |
| 10 | + const url_with_qs = `${url_no_qs}?${query_string}`; |
| 11 | + const home_url = `${website_url}${language}/home.html`; |
| 12 | + |
| 13 | + describe('.paramsHash()', () => { |
| 14 | + it('returns correct object', () => { |
| 15 | + expect(Url.paramsHash(url_with_qs)).to.be.an('Object') |
| 16 | + .and.to.have.all.keys('market', 'duration_amount', 'no_value') |
| 17 | + .and.to.deep.equal(params_obj); |
| 18 | + }); |
| 19 | + it('returns empty object when there is no query string', () => { |
| 20 | + expect(Url.paramsHash(url_no_qs)).to.be.an('Object') |
| 21 | + .and.to.deep.equal({}); |
| 22 | + expect(Url.paramsHash(`${url_no_qs}?`)).to.be.an('Object') |
| 23 | + .and.to.deep.equal({}); |
| 24 | + setURL(url_no_qs); |
| 25 | + expect(Url.paramsHash()).to.deep.eq({}); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('.getLocation()', () => { |
| 30 | + it('works as expected', () => { |
| 31 | + expect(Url.getLocation().hostname).to.eq('www.binary.com'); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('.paramsHashToString()', () => { |
| 36 | + it('returns an empty string if empty object or undefined passed', () => { |
| 37 | + expect(Url.paramsHashToString({})).to.eq(''); |
| 38 | + expect(Url.paramsHashToString()).to.eq(''); |
| 39 | + }); |
| 40 | + it('returns the expected conversion of object to string', () => { |
| 41 | + expect(Url.paramsHashToString(params_obj)).to.eq(query_string); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('.urlFor()', () => { |
| 46 | + it('returns home as default', () => { |
| 47 | + [undefined, null, '', '/', 'home'].forEach((path) => { |
| 48 | + expect(Url.urlFor(path)).to.eq(home_url); |
| 49 | + }); |
| 50 | + }); |
| 51 | + it('accepts params', () => { |
| 52 | + expect(Url.urlFor('trading', query_string)).to.eq(url_with_qs); |
| 53 | + }); |
| 54 | + it('returns the correct language', () => { |
| 55 | + expect(Url.urlFor('home', undefined, 'es')).to.eq(`${website_url}es/home.html`); |
| 56 | + }); |
| 57 | + it('ignores invalid characters', () => { |
| 58 | + expect(Url.urlFor('`~!@#$%^&*)(=+\[}{\]\\\"\';:\?><,|')).to.eq(home_url); |
| 59 | + }); |
| 60 | + it('handles all valid characters', () => { |
| 61 | + expect(Url.urlFor('metatrader/comparison-4_vs_5')) |
| 62 | + .to.eq(`${website_url}${language}/metatrader/comparison-4_vs_5.html`); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('.urlForStatic()', () => { |
| 67 | + it('returns base path as default', () => { |
| 68 | + expect(Url.urlForStatic()).to.eq(website_url); |
| 69 | + }); |
| 70 | + it('returns expected path', () => { |
| 71 | + expect(Url.urlForStatic('images/common/plus.svg')).to.eq(`${website_url}images/common/plus.svg`); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('.param()', () => { |
| 76 | + it('returns undefined if no match', () => { |
| 77 | + setURL(url_with_qs); |
| 78 | + expect(Url.param()).to.eq(undefined); |
| 79 | + }); |
| 80 | + it('returns expected parameter', () => { |
| 81 | + expect(Url.param('duration_amount')).to.be.a('string').and.eq('5'); |
| 82 | + expect(Url.param('no_value')).to.be.a('string').and.eq(''); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('.websiteUrl()', () => { |
| 87 | + it('returns expected value', () => { |
| 88 | + expect(website_url).to.eq('https://www.binary.com/'); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments