Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
"use strict";
const { ShoppingProduct, isProductURL } = ChromeUtils.importESModule(
"chrome://global/content/shopping/ShoppingProduct.mjs"
);
add_task(function test_product_fromUrl() {
Assert.deepEqual(
ShoppingProduct.fromURL(),
{ valid: false },
"Passing a nothing returns empty result object"
);
Assert.deepEqual(
ShoppingProduct.fromURL(12345),
{ valid: false },
"Passing a number returns empty result object"
);
Assert.deepEqual(
{ valid: false },
"String urls returns empty result object"
);
Assert.deepEqual(
{ host: "mozilla.org", valid: false },
"Invalid Url returns a full result object"
);
Assert.deepEqual(
.host,
"walmart.com",
"WWW in host is ignored"
);
Assert.deepEqual(
ShoppingProduct.fromURL(
),
{ host: "staging.walmart.com", valid: false },
"Subdomain in valid Product Url returns partial result"
);
Assert.deepEqual(
{ host: "walmart.co.uk", sitename: "walmart", valid: false },
"Invalid in Product TLD returns partial result"
);
Assert.deepEqual(
{ host: "walmart.com", sitename: "walmart", tld: "com", valid: false },
"Non-Product page returns partial result"
);
Assert.deepEqual(
{
host: "walmart.com",
sitename: "walmart",
tld: "com",
id: "926485654",
valid: true,
},
"Valid Product Url returns a full result object"
);
Assert.deepEqual(
{
host: "walmart.com",
sitename: "walmart",
tld: "com",
id: "926485654",
valid: true,
},
"Protocol is not checked"
);
Assert.deepEqual(
{
host: "amazon.fr",
sitename: "amazon",
tld: "fr",
id: "ABCDEFG123",
valid: true,
},
"Valid French Product Url returns a full result object"
);
Assert.deepEqual(
{
host: "amazon.de",
sitename: "amazon",
tld: "de",
id: "ABCDEFG123",
valid: true,
},
"Valid German Product Url returns a full result object"
);
});
add_task(function test_product_isProduct() {
let product = {
host: "walmart.com",
sitename: "walmart",
tld: "com",
id: "926485654",
valid: true,
};
Assert.equal(
ShoppingProduct.isProduct(product),
true,
"Passing a Product object returns true"
);
Assert.equal(
ShoppingProduct.isProduct({ host: "walmart.com", sitename: "walmart" }),
false,
"Passing an incomplete ShoppingProduct object returns false"
);
Assert.equal(
ShoppingProduct.isProduct(),
false,
"Passing nothing returns false"
);
});
add_task(function test_amazon_product_urls() {
let product;
let url_com = new URL(
);
let url_ca = new URL(
);
let url_uk = new URL(
);
product = ShoppingProduct.fromURL(url_com);
Assert.equal(ShoppingProduct.isProduct(product), true, "Url is a product");
Assert.equal(product.id, "B09TJGHL5F", "Product id was found in Url");
product = ShoppingProduct.fromURL(url_ca);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a supported tld"
);
product = ShoppingProduct.fromURL(url_uk);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a supported tld"
);
product = ShoppingProduct.fromURL(url_content);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a product"
);
});
add_task(function test_walmart_product_urls() {
let product;
let url_com = new URL(
);
let url_ca = new URL(
);
let url_content = new URL(
);
product = ShoppingProduct.fromURL(url_com);
Assert.equal(ShoppingProduct.isProduct(product), true, "Url is a product");
Assert.equal(product.id, "823391155", "Product id was found in Url");
product = ShoppingProduct.fromURL(url_ca);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a valid tld"
);
product = ShoppingProduct.fromURL(url_content);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a product"
);
});
add_task(function test_bestbuy_product_urls() {
let product;
let url_com = new URL(
);
let url_ca = new URL(
);
let url_content = new URL(
);
product = ShoppingProduct.fromURL(url_com);
Assert.equal(ShoppingProduct.isProduct(product), true, "Url is a product");
Assert.equal(product.id, "6530134.p", "Product id was found in Url");
product = ShoppingProduct.fromURL(url_ca);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a valid tld"
);
product = ShoppingProduct.fromURL(url_content);
Assert.equal(
ShoppingProduct.isProduct(product),
false,
"Url is not a product"
);
});
add_task(function test_isProductURL() {
let product_string =
let product_url = new URL(product_string);
let product_uri = Services.io.newURI(product_string);
Assert.equal(
isProductURL(product_url),
true,
"Passing a product URL returns true"
);
Assert.equal(
isProductURL(product_uri),
true,
"Passing a product URI returns true"
);
let content_string =
let content_url = new URL(content_string);
let content_uri = Services.io.newURI(content_string);
Assert.equal(
isProductURL(content_url),
false,
"Passing a content URL returns false"
);
Assert.equal(
isProductURL(content_uri),
false,
"Passing a content URI returns false"
);
Assert.equal(isProductURL(), false, "Passing nothing returns false");
Assert.equal(isProductURL(1234), false, "Passing a number returns false");
Assert.equal(
isProductURL("1234"),
false,
"Passing a junk string returns false"
);
});
add_task(function test_new_ShoppingProduct() {
let product_string =
let product_url = new URL(product_string);
let product_uri = Services.io.newURI(product_string);
let productURL = new ShoppingProduct(product_url);
Assert.equal(
productURL.isProduct(),
true,
"Passing a product URL returns a valid product"
);
let productURI = new ShoppingProduct(product_uri);
Assert.equal(
productURI.isProduct(),
true,
"Passing a product URI returns a valid product"
);
});