diff --git a/packages/commerce-sdk-react/mock-responses/usepromotions-returns-data.json b/packages/commerce-sdk-react/mock-responses/usepromotions-returns-data.json new file mode 100644 index 0000000000..29afee4fe3 --- /dev/null +++ b/packages/commerce-sdk-react/mock-responses/usepromotions-returns-data.json @@ -0,0 +1,159 @@ +[ + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=IcXVMRMyiVOCuwwh_CfzamM_UmKGCcYeNqw8_Nx4Ed4", + "body": "", + "status": 303, + "response": "", + "rawHeaders": [ + "date", + "Tue, 11 Oct 2022 20:46:09 GMT", + "content-length", + "0", + "connection", + "close", + "location", + "http://localhost:3000/callback?usid=a567c1d0-b035-40af-afdc-448f3f008644&code=nTKpEEF9Vznera_OJzn2_wLO1VqjIYLiyJYkXvfkpFc", + "cf-ray", + "758a718bbdd41b1e-PHX", + "cache-control", + "no-store", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "758a718bbdd41b1e", + "x-ratelimit-1m-limit", + "24000, 180000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23995, 179909, 23999, 358701", + "x-ratelimit-1m-reset", + "50703, 50702, 50702, 50700", + "vary", + "Accept-Encoding", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=IcXVMRMyiVOCuwwh_CfzamM_UmKGCcYeNqw8_Nx4Ed4" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/callback?usid=a567c1d0-b035-40af-afdc-448f3f008644&code=nTKpEEF9Vznera_OJzn2_wLO1VqjIYLiyJYkXvfkpFc", + "body": "", + "status": 200, + "response": "", + "rawHeaders": [ + "Date", + "Tue, 11 Oct 2022 20:46:09 GMT", + "Connection", + "close", + "Transfer-Encoding", + "chunked" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "POST", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token", + "body": "client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&code=nTKpEEF9Vznera_OJzn2_wLO1VqjIYLiyJYkXvfkpFc&code_verifier=E4MfA5rgR3oSw-dyLg_vcjcq_b8GuhxtfRXFD7JIhXe9D14VOlxYssCzRAvC-CSHhCuc5Z0r5Z9429BHRABRcwfXCydGH4trOheE8ND0DhHtIMJsr_EbY3KF3R3Rp0to&grant_type=authorization_code_pkce&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&usid=a567c1d0-b035-40af-afdc-448f3f008644", + "status": 200, + "response": [ + "1f8b08000000000000008d55cb76a33810ddf757e4643d992309e3c7ec6cb03104449bd880b4f1010987771c83cda3cffcfb08a7bb2733edeef4ca5671abead6ad52e9cba7bbbbfb80b1a8aaf6f54b1695f7777fdddd479d81a8af2776a2b7663a4f4cc5780da49d381b7da8ad7a535a7421da5ca2f2d8d17cda58ea72c09c026f3360328c8c84aa714c15d0937ed9995b27b3b54d6d6fdd0227a0a56ad68aff29dd66d0da92c11744fe7cf07de5d26638a7e19a0ce7472b19f5667ab5c5a1c606db6ad7eb1027c6ec4f4134e6fe35e9dadde54b3d69128670a3a72fe297a6a493fb409b354c733bd38332f1701a4aee992b72427c7ca2bed3eb6b5c10649d198a2f6cbdc85902ebc85ba504cd60586ecec4733256b83d43ee2d6cc38a59c63d0c58b7e8698153b3c42fa1b468848035d5f25ec411621ab2e9e39cf8461a283fe2423f8f09c217eec9c02c0de9234e4c5bc9a127ead0f233939c8e7b300fcb9bb5a4dcc720443067a5750eb5591978a3e70fb4b9b0c219f03770188a7c1791bb639ddc518f1fc559e4f9b12652acfa00b94217f88b78ffc91b53cde9a88f7bea8b1a4af611febbf6a1d099af7fe2e30fc3ea40223955e0dfee135b1ba2a61908bcd9f9d6f71bb5fc163fe2e735153d0d3c59f033e0b5673fe329b4a4e5a626c8ed445f8bc0c331d77e3d73bf3f531f6b40d00a887e8afaf2fcd6f7f7f1aef74c7293e19e096e35436dccfa97f1bb8530b64b9c8a8530267d9e62d54d68b1a92dcdcdac0e00e20d4b219371bfacb1ba286cf559c6a991d8aa85ecf40819ca33bb58429cf2d4f236", + "8de9e98dd5ef066c4c1318d3c249cded06d81ae9693a6f6c9500ace8955e601029fa582f71457c6b581a623ead8f1697c0881d922e85ff0ad2c1bfc0621ec49d2984bea815356f2ec38c5065266a340abf9f37d6d3904f4e68a28fc5124378bbebacedb2b79f066d1c79d0c615fd61c980137b201978b947aa4dc7dff4e2fee26c671c8a990743dd81e77c3feb2b57dc037d6c17fc4dc7024b62ee9a505d9cdc324e886b484fda8a84eb15f5bc3aa7cb4a60f1cb80d5af5ab823a6bce3966668e016782b20b8b53825106ff5567c978705cfb56cd049b5320bd8ea0e59e9bc15ba36e67639b2b77384fb67d952add64a455ffa4cf8ebc0daee245bd5654b11cbf8790a59abc9fe14cd7697b9acecb1a52afb31d60bf8bc3115d53c1ccab9f7b9755eb38c4c1e4323516c597b08aa6a14766b9356f3daf58e5b3e9de91a9a689d013fef9f9afb3f86272ae1ef9ea737d3293a9ca22a7e67bf00f834d1509f559ffd47670fb81657393b9a73eeee89921e51379e824a8839d7df4244ed311131f6c9d51f4e01b89aaf11f775778cae6117cbb9b374de3cce55c2afc6401e4f18e4e0210492fc3002c1e1213870f6301a4d0fd20180e978347a7361e7aa7e29a2d3feab67c89a53057290b96d1838cd3af0f35742b2883f7e2555b2fdb9fa171ff0702ac16072e07c2a710e23144860c6a2893c83fc70987e13e8b8ffff3b5e9ef3fcd3dfff005b9fa1fae3070000" + ], + "rawHeaders": [ + "date", + "Tue, 11 Oct 2022 20:46:09 GMT", + "content-type", + "application/json", + "transfer-encoding", + "chunked", + "connection", + "close", + "cf-ray", + "758a718e2cd3a726-PHX", + "cache-control", + "no-store", + "content-encoding", + "gzip", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "vary", + "Accept-Encoding, User-Agent", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "758a718e2cd3a726", + "x-ratelimit-1m-limit", + "24000, 180000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23994, 179908, 23998, 358652", + "x-ratelimit-1m-reset", + "50311, 50310, 50310, 50309", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions?siteId=RefArchGlobal&ids=10offsuits%2C50%25offorder", + "body": "", + "status": 200, + "response": [ + "1f8b0800000000000003000000ffff7c90c10ac2301044eff98a3150bcf450855efa0f7e8178084da281341b9a2d2aa5ff2e8d95a2a297856567e62d330a407ad739960df6e5bc69c54a36380a0018f30464abbca7810fe92c1bc85d5580ac4567c236210d8e13ae8e2f6869881464f9b269c3caf9347bccad35911d850415a3bfaf22a79f99646d8e5a2f4175e6074f66cd54fe7bb35e6cd46bd36f3e81755590b5f9f64d7cb32e2c019c72454cacfc5c98981e000000ffff03004cdfc37141010000" + ], + "rawHeaders": [ + "date", + "Tue, 11 Oct 2022 20:46:10 GMT", + "content-type", + "application/json; charset=UTF-8", + "transfer-encoding", + "chunked", + "connection", + "close", + "cf-ray", + "758a7191dc6da6ea-PHX", + "cache-control", + "public, must-revalidate, max-age=3600", + "strict-transport-security", + "max-age=31536000; includeSubdomains;", + "cf-cache-status", + "DYNAMIC", + "x-content-type-options", + "nosniff", + "x-correlation-id", + "758a7191dc6da6ea", + "x-frame-options", + "SAMEORIGIN", + "x-ratelimit-limit", + "800", + "x-ratelimit-remaining", + "800", + "x-xss-protection", + "1; mode=block", + "vary", + "Accept-Encoding", + "server", + "cloudflare", + "content-encoding", + "gzip", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions?siteId=RefArchGlobal&ids=10offsuits%2C50%25offorder" + ], + "responseIsBinary": false + } +] \ No newline at end of file diff --git a/packages/commerce-sdk-react/mock-responses/usepromotions-returns-error.json b/packages/commerce-sdk-react/mock-responses/usepromotions-returns-error.json new file mode 100644 index 0000000000..74096f47e4 --- /dev/null +++ b/packages/commerce-sdk-react/mock-responses/usepromotions-returns-error.json @@ -0,0 +1,147 @@ +[ + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=orjIUis8M7aq923GmDxgGD3osqpJo1OYK5wA08Fd0cM", + "body": "", + "status": 303, + "response": "", + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 22:47:56 GMT", + "content-length", + "0", + "connection", + "close", + "location", + "http://localhost:3000/callback?usid=58cb74cf-f2f4-4c63-b82d-b7186971a279&code=70jO2bFuQGv2xWu5dLzeyRjUqDhIrc95ieK035NwseA", + "cf-ray", + "759361536be6a6f6-PHX", + "cache-control", + "no-store", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "759361536be6a6f6", + "x-ratelimit-1m-limit", + "24000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23985, 23999, 350799", + "x-ratelimit-1m-reset", + "3088, 3087, 3086", + "vary", + "Accept-Encoding", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=orjIUis8M7aq923GmDxgGD3osqpJo1OYK5wA08Fd0cM" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/callback?usid=58cb74cf-f2f4-4c63-b82d-b7186971a279&code=70jO2bFuQGv2xWu5dLzeyRjUqDhIrc95ieK035NwseA", + "body": "", + "status": 200, + "response": "", + "rawHeaders": [ + "Date", + "Wed, 12 Oct 2022 22:47:57 GMT", + "Connection", + "close", + "Transfer-Encoding", + "chunked" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "POST", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token", + "body": "client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&code=70jO2bFuQGv2xWu5dLzeyRjUqDhIrc95ieK035NwseA&code_verifier=WI1F2HOWj5m2BNpjMHGaK475KC899ZWJ4xuQa2mEcZcRnnOGjRZKGZ4DEUtI5FvVcpvcgC7VY6dodOesg9xGrB6oPoJ-Y6ERuMFNwscjVuAJO_U5mfoKNxTqXIzvzy7V&grant_type=authorization_code_pkce&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&usid=58cb74cf-f2f4-4c63-b82d-b7186971a279", + "status": 200, + "response": [ + "1f8b08000000000000008d554d97a23814ddcfafa853eba939104465762a8a2084915242b2f14082f2ad55820873e6bf4f6275f771a6edae5ee97bdcf775f372f3f76f4f4fcf21a5f1f9bcab8f795c3d3ffdf9f41c7716208199baa979b5b3496acfacb750d972dbea2363d1dbcab48bc0fa1257a78e14e3d6d1e702f31ea2b5c0e4105829d19384cca41ef7f3cede78b96bac6b77e3973095ae44cfaffc7f4636b9ec6cb08895e2602262df98b21676162db1b0574e3ae8edece64b22830adf62db9b324c2ded0fde68c2825bd1a5bf2de666daa614c0d6cc8efc9764b853fbd0d05a6af89d8d64152398458adfb0999ae200be93c0ebcd252c31701a0a920b5d4e0b9aca758c1619069a1c55eb06232fa7a5df53e03fc2b6b4d47286a044bb694f4a98d9153c46cab4e504d6c4287a9e879369a976000b1c585938fb1e1705458201bc30a44a7665299ff5448d851a213e87513454f13a86e422aa1ece92b1004a11900b5a394d6468558806874fb8b9d0d213f8073828f37a175ebba39dda11c44edce675be9f09978b3e043ee745fe49beffd44d88e17524803d09f80c15fd0cff8dfb88f3cc963f8809c4b27a3256bc73183c3e27bab4f84c9a1422ad79f4fdc12cbfd41f0e8a9af0330d91cafbb3e4db99fda84fce25a9d635067ec7cfb50c114c98f1f39dfbf59dfa9c030c16123f4f3e5f513cfa7e9fef76cf143f15f78cf75653704d687f1cde09c2d0ad60c6056188fb2283ba9f92725d3b869f3b9d246124442157613fafa13e2d5dfda0c2cc4a5ddd016e769229287237db0e30301568c0d246b823d9ba8686039c4ee6382bb791a9389b037037f48a37a6e2be9a67b384523c33876605cf38708468f0fd743e132e8ee11a92cd79fc422622be847c1ff89d2939bfe0ca675e5fc48e9099c667b4caa09fb4cead9e9a92d41c7211037083af503f", + "48b013dc78aae0c6e7e7435381e33a908abefc1331c6c3af7cb160dab83993f9ce4b62ee1079df6c73e1f37b600edd927df088ac33cd89c9a4648a02da32d91abc1a89cde722286098c9078e8547813567a2a63fa0b3bbde32ac88de42b490786fc2966136975c9d2a42e099910b9e742777b86f0b9c6c727575dcda9bf9c0dd4c00ec0faaa33b572733856877509f709be716d88e8bf1c1df1a2fafc1da3ebf2aa7fe182a86ffd635e9617f6a71a842d08e9a055979109f94b7d1fcd05a75b7238e761a36abeca222354ad0b832dbe4b241bdb7920a57b9ecb3c9f3efe2894ad9ddf3f4e17a8ff7eff139b9f3e787a3335ed864db6e735b896d535ad2d65b2fd12a58fdd5eca40b56c778d1a09d76f848115f4f29cfb14b6ff1f258926eee5bc65ddd9de25bdae97ce2cdbd8f88e69cb29b531dd36834a0fb973dd80f5e0674a8bc4463c05ea2913c1e6a23390423ed238436e7fa58c6efbb2f916154bc2f96ed72825ba9f5aecb64558418b7411b7c69aaa2bbe67c8767d15891c3d19eb1b1c2981c835091341a8f544d66fbfdf82b41a7ddffdff1aa298adffef917c7c0979de3070000" + ], + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 22:47:57 GMT", + "content-type", + "application/json", + "transfer-encoding", + "chunked", + "connection", + "close", + "cf-ray", + "759361584f69a715-PHX", + "cache-control", + "no-store", + "content-encoding", + "gzip", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "vary", + "Accept-Encoding, User-Agent", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "759361584f69a715", + "x-ratelimit-1m-limit", + "24000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23984, 23998, 350719", + "x-ratelimit-1m-reset", + "2460, 2459, 2459", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions?siteId=RefArchGlobal&ids=promo_10%2Cpromo_30%2Cpromo_42%2Cpromo_46%2Cpromo_21%2Cpromo_22%2Cpromo_13%2Cpromo_12%2Cpromo_20%2Cpromo_44%2Cpromo_10%2Cpromo_1%2Cpromo_22%2Cpromo_0%2Cpromo_23%2Cpromo_14%2Cpromo_36%2Cpromo_16%2Cpromo_2%2Cpromo_0%2Cpromo_31%2Cpromo_41%2Cpromo_32%2Cpromo_24%2Cpromo_11%2Cpromo_26%2Cpromo_36%2Cpromo_49%2Cpromo_23%2Cpromo_9%2Cpromo_30%2Cpromo_0%2Cpromo_46%2Cpromo_43%2Cpromo_4%2Cpromo_46%2Cpromo_14%2Cpromo_13%2Cpromo_2%2Cpromo_30%2Cpromo_43%2Cpromo_33%2Cpromo_47%2Cpromo_21%2Cpromo_32%2Cpromo_15%2Cpromo_26%2Cpromo_36%2Cpromo_41%2Cpromo_41%2Cpromo_14", + "body": "", + "status": 400, + "response": { + "title": "Bad Request", + "type": "https://api.commercecloud.salesforce.com/documentation/error/v1/errors/invalid-query-parameter", + "detail": "Invalid value 'promo_10,promo_30,promo_42,promo_46,promo_21,promo_22,promo_13,promo_12,promo_20,promo_44,promo_10,promo_1,promo_22,promo_0,promo_23,promo_14,promo_36,promo_16,promo_2,promo_0,promo_31,promo_41,promo_32,promo_24,promo_11,promo_26,promo_36,promo_49,promo_23,promo_9,promo_30,promo_0,promo_46,promo_43,promo_4,promo_46,promo_14,promo_13,promo_2,promo_30,promo_43,promo_33,promo_47,promo_21,promo_32,promo_15,promo_26,promo_36,promo_41,promo_41,promo_14' for query parameter ids. expected maxLength: 256, actual: 450" + }, + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 22:47:58 GMT", + "content-type", + "application/problem+json; charset=UTF-8", + "content-length", + "680", + "connection", + "close", + "cf-ray", + "7593615accd2a6f6-PHX", + "strict-transport-security", + "max-age=31536000; includeSubdomains;", + "cf-cache-status", + "DYNAMIC", + "x-correlation-id", + "7593615accd2a6f6", + "Vary", + "Accept-Encoding", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions?siteId=RefArchGlobal&ids=promo_10%2Cpromo_30%2Cpromo_42%2Cpromo_46%2Cpromo_21%2Cpromo_22%2Cpromo_13%2Cpromo_12%2Cpromo_20%2Cpromo_44%2Cpromo_10%2Cpromo_1%2Cpromo_22%2Cpromo_0%2Cpromo_23%2Cpromo_14%2Cpromo_36%2Cpromo_16%2Cpromo_2%2Cpromo_0%2Cpromo_31%2Cpromo_41%2Cpromo_32%2Cpromo_24%2Cpromo_11%2Cpromo_26%2Cpromo_36%2Cpromo_49%2Cpromo_23%2Cpromo_9%2Cpromo_30%2Cpromo_0%2Cpromo_46%2Cpromo_43%2Cpromo_4%2Cpromo_46%2Cpromo_14%2Cpromo_13%2Cpromo_2%2Cpromo_30%2Cpromo_43%2Cpromo_33%2Cpromo_47%2Cpromo_21%2Cpromo_32%2Cpromo_15%2Cpromo_26%2Cpromo_36%2Cpromo_41%2Cpromo_41%2Cpromo_14" + ], + "responseIsBinary": false + } +] \ No newline at end of file diff --git a/packages/commerce-sdk-react/mock-responses/usepromotionsforcampaign-returns-data.json b/packages/commerce-sdk-react/mock-responses/usepromotionsforcampaign-returns-data.json new file mode 100644 index 0000000000..5d397675b0 --- /dev/null +++ b/packages/commerce-sdk-react/mock-responses/usepromotionsforcampaign-returns-data.json @@ -0,0 +1,160 @@ +[ + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=_KNksH2sBJTPRtCIhaNZxFkA8OJZD8iyY_B0oMGiNRk", + "body": "", + "status": 303, + "response": "", + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 23:49:32 GMT", + "content-length", + "0", + "connection", + "close", + "location", + "http://localhost:3000/callback?usid=96637591-1ae8-4b97-a8f6-bd40b5405ccd&code=ArP2Alu3SIM8kRvA_muSziSpJqU-0QmBrR27w0lYtbg", + "cf-ray", + "7593bb8d7cfea720-PHX", + "cache-control", + "no-store", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "7593bb8d7cfea720", + "x-ratelimit-1m-limit", + "24000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23985, 23999, 354209", + "x-ratelimit-1m-reset", + "27538, 27537, 27536", + "vary", + "Accept-Encoding", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=_KNksH2sBJTPRtCIhaNZxFkA8OJZD8iyY_B0oMGiNRk" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/callback?usid=96637591-1ae8-4b97-a8f6-bd40b5405ccd&code=ArP2Alu3SIM8kRvA_muSziSpJqU-0QmBrR27w0lYtbg", + "body": "", + "status": 200, + "response": "", + "rawHeaders": [ + "Date", + "Wed, 12 Oct 2022 23:49:32 GMT", + "Connection", + "close", + "Transfer-Encoding", + "chunked" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "POST", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token", + "body": "client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&code=ArP2Alu3SIM8kRvA_muSziSpJqU-0QmBrR27w0lYtbg&code_verifier=HI-3eazOVPd6g_YsqowQhBDyeAuZbtJcmvOXdVwxR3CEx3g7okgWzGlQpBRVxvgF6bb6Qf0WvGZ9dDk2qXMqjmHomfWUpukVQlTqAicfGCM5T9pvGPvsVZwDMXI32Hrg&grant_type=authorization_code_pkce&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&usid=96637591-1ae8-4b97-a8f6-bd40b5405ccd", + "status": 200, + "response": [ + "1f8b08000000000000008d55cb72a33814ddf757a4b29e4c21308e999d1f802146b40906a48d0b2462c4cb8e0dc63035ff3e92d39dca4cbb3bbd025deeeb9c7b75f8fbcbdddd7d4c487a3a6d9b7d91d6f7777fdddda7bd2de3c8622eb32eab7cca5673fb355636fc6c0f89690c2b65d627f2fa9cd6871e9793ce59e8c2e718876be15340d966789165782e0d68d0fb95ef15aeb96e5c3fa820932e78515cf87b8efd02383e12b1521a4d45ec2b55d6e29c274b24ce4f0e1b0dabfc6acb1293089bb1192c0099adfdc91bcd68742dba0c36a56eb18e11197656bee74f9ca35e1d6253eb8819f4ab10a8288479a2042d9dab0c45f088236fb096b042b2d312393b93e5ac240c346968e448d64052af5b147a05a98281c8c12ddf8e545a414328917e36e00ae6ab1aee1365d671021b6c9603cfc3c9b4d555044b14d9793cffd12f89ca0cc9f04c43555ad5b6f2594fc434d424e438ccb2258ad7d31094497d134b4e232825322849edb489a9d57138da7dc2cd99549ef0bfe10701af77e6b57bd2ab3d0ee9819f799d1f31a1ca186239e0bc805fe4fb4fdd0c9b5e8f2338e08863a8c967feefdc279c67bafc494c2496d50348f14e71747b4e6469734c9a14875a7bebfb0d2cbfd51f8aca06f399c6a1cafbb3c175663feb937389eb7583e4a0e773ade21066d4fcf5cefdfe4e7dce01920d89cf93e32bcb5bdf3fe6bbde332560e29ef1de1a225f3232ecc71f0461ecd630e78230464399c345c070b56e1c33289c5e92502844a150e1a0377031abdcc54e85b9cddc8523bbf90110b92cdcbc9061ee28d02f2e2bdfc8b0bf6ba069a9b007996b22791572a159cc18f4d71de49ce3b975b22a28a5736b6cd5f08422478806df4fe733e1e23e5c43729dc71b008bf80af27de077a6e2fcca178e797d163b82e71ac76857d130ed9c67514f659859632e6232", + "f4d1c55d6c24a717dc78aae026e0f3214cf8711d60a2afe080cdc9f83b5f349ab56e4101df7949e08e43effd6c1901bf07d6d8ade81b8fa1ddf0bde96290cdc20af408d8471a7a4f4968e030a2d6b3cf39afe05ef85a572e821199bff7d63b0b4711bdc5a121f1de2e304700e6fa08fa6410024fcd42f0b4700a47e23864279f723ca85bf9fac8f5a7321c762acfd13bc34675068bbf23050e1b9e633a82cf5c8ca7cbec7caaf59d390a0753e7042ff781149c3af2d40ccd4a618e6468f3cbd662bbb64d8cd94e6d28d471a56afbc7d98e7acfd56bb3dcd7f9288ab7735951774768af8beefe0ff18b62f4c3efe9cd744c5f8ee929fb6007cf4fd56563481bdd64e1d4710eac8f3d34e06c20065d7c3d9f37a1b97d7d623389bca5482f07c6736cd9351e4c24e96abe66dc36fd21bda69de9534ff7de22da13a357a3361e2b8faa061e409c4e1e4689f6f8104f5ec60f091d49893a925442e85b08694fcdbe4a8fdb6f9171521d3ba988a6b83a12af68a95dc508754b0b7c6baa26dbf6f4c19f261305c48f2f944e144a412ac78aa4915454a72f2f93ef041db6ffff8fd76d597ef9e75f277b780be3070000" + ], + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 23:49:33 GMT", + "content-type", + "application/json", + "transfer-encoding", + "chunked", + "connection", + "close", + "cf-ray", + "7593bb913f2f1b14-PHX", + "cache-control", + "no-store", + "content-encoding", + "gzip", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "vary", + "Accept-Encoding, User-Agent", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "7593bb913f2f1b14", + "x-ratelimit-1m-limit", + "24000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23984, 23999, 354126", + "x-ratelimit-1m-reset", + "26948, 26948, 26946", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions/campaigns/promotion-campaign?siteId=RefArchGlobal", + "body": "", + "status": 200, + "response": [ + "1f8b0800000000000003000000ffff", + "ac523b6bc33010defd2bbe0a42170764172fde02593bb5534b07d5961281ac13d6b90f42fe7bb152631a9a52d32e0271dff3b8430608673bcba2c64d3efe5ac54ad478cc00e0905e4034ca391af836ee440d51c815c81874da5f47c4c172c4abe53d1a1a0279914fb456b3b22e8e1cfdd6e8c0967c840ac1bdcf20eddbad623d824a5996eb42aecbea5eca5aca871965db933319930ce789579dbe906a0645563d9fdb14c5649360c7fca7d2d5a7fcddde8660fd0e9b8e06cfd83cd38b4621e5b7b517b096eca1922b3266123d6926c92f8ad3661684f8bf3d51dfeafeea2ff592c2e53667e35f25cf80a774e64cacdc78f4d9f1030000ffff0300718bc69205030000" + ], + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 23:49:33 GMT", + "content-type", + "application/json; charset=UTF-8", + "transfer-encoding", + "chunked", + "connection", + "close", + "cf-ray", + "7593bb93d87e1b19-PHX", + "cache-control", + "public, must-revalidate, max-age=60", + "strict-transport-security", + "max-age=31536000; includeSubdomains;", + "cf-cache-status", + "DYNAMIC", + "x-content-type-options", + "nosniff", + "x-correlation-id", + "7593bb93d87e1b19", + "x-frame-options", + "SAMEORIGIN", + "x-ratelimit-limit", + "800", + "x-ratelimit-remaining", + "800", + "x-xss-protection", + "1; mode=block", + "vary", + "Accept-Encoding", + "server", + "cloudflare", + "content-encoding", + "gzip", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions/campaigns/promotion-campaign?siteId=RefArchGlobal" + ], + "responseIsBinary": false + } +] \ No newline at end of file diff --git a/packages/commerce-sdk-react/mock-responses/usepromotionsforcampaign-returns-error.json b/packages/commerce-sdk-react/mock-responses/usepromotionsforcampaign-returns-error.json new file mode 100644 index 0000000000..a0ac0ad1af --- /dev/null +++ b/packages/commerce-sdk-react/mock-responses/usepromotionsforcampaign-returns-error.json @@ -0,0 +1,147 @@ +[ + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=gumSJ99RYfjbt4EZHm4R0TPlJ9BC1IoH0uarxcoLkHE", + "body": "", + "status": 303, + "response": "", + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 23:50:12 GMT", + "content-length", + "0", + "connection", + "close", + "location", + "http://localhost:3000/callback?usid=f5e82ada-ed74-4a1a-9dfd-c716b3b7ae8e&code=dRDuGEitC6pdw1wUcRDgfHByP_UgBV7oIB_aU4P6iwY", + "cf-ray", + "7593bc88189fa70c-PHX", + "cache-control", + "no-store", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "7593bc88189fa70c", + "x-ratelimit-1m-limit", + "24000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23995, 23999, 357832", + "x-ratelimit-1m-reset", + "47446, 47445, 47444", + "vary", + "Accept-Encoding", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&response_type=code&client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&hint=guest&code_challenge=gumSJ99RYfjbt4EZHm4R0TPlJ9BC1IoH0uarxcoLkHE" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/callback?usid=f5e82ada-ed74-4a1a-9dfd-c716b3b7ae8e&code=dRDuGEitC6pdw1wUcRDgfHByP_UgBV7oIB_aU4P6iwY", + "body": "", + "status": 200, + "response": "", + "rawHeaders": [ + "Date", + "Wed, 12 Oct 2022 23:50:12 GMT", + "Connection", + "close", + "Transfer-Encoding", + "chunked" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "POST", + "path": "/mobify/proxy/api/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token", + "body": "client_id=c9c45bfd-0ed3-4aa2-9971-40f88962b836&code=dRDuGEitC6pdw1wUcRDgfHByP_UgBV7oIB_aU4P6iwY&code_verifier=h0LY_71NXYp3Elteodn5-SLFKKr6nCl8WmX8SJ0QrXMua1eNZqDpVqOYReERmrg8CBLBZcGdg9B6nvEy0Gs1m1Y4yamVXZKXEDIF_ZvoBbHY-zCxiwI37hda9WlnREDY&grant_type=authorization_code_pkce&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&usid=f5e82ada-ed74-4a1a-9dfd-c716b3b7ae8e", + "status": 200, + "response": [ + "1f8b08000000000000008d55cb72a33814ddf757a4b29e4c2130b6999d5f6008126de220a44d0a246cc4cb8e6d6c606afe7d84d39dca4cbb3bbd70615dce7d9c73af2e7f7fb9bbbb8f184b8ec797d32e4faafbbbbfeeee93d65169680b4fd88d9b4d843b735e23ed599e9d2eb6ccced5a66dacaece49b56f6931bec0f9a2c71c22bcea3139521d41e7694a674a47ba45ebaefddcb356276f1d9448280d9de78dfc9fd1750ee09af4be4a124e7adf57aeadfa73162f497f7e8462d0b9d9d596c616eb6de6736703241ce34f5968cac36bd265f05c2c6c71114c45173bdbc927cd48ab7791655c9815b42e063ac1288bb5a0e6335d90101d68e877f6129544853553d3335b4e0b26c029c166465403c4d5aa26d8cf5919744c0d6e612fac34728e91c2da69474b94b915dac5daf422053c51abe8641c29a6a3bb212a48e864d1ec475c1c162951d199635d712b47fbac2666997a8c250faba899e6b71c8322ae6e72c9788894580505ab601d5b4615e1c1f6136dceacf47bfc0d1c0232df59e66e59abb714f3bd3ccb3c3f7222a5d9456a207501bf88f79fbc29b5fc9686a8a3a1e450b1cff0efdac75267befc894fd80fab0f88e61fa3f0769fd8d2919c0c25c2467debfd0d2ebf551f098b13953d8db02eeb73c0b5673fab536a49abd589a8412bfb5a4618a5dcfaf5ccfdfe4c7dae01514d45f653f22b8a5bef3fc6bbde332d10fd3d93b59d98daa4acdb0d3f2c84a157a14c2e8421e98a0ccd0341cbd5095a410e5b4521b85f0ab98ebac509cda7a537dfea2873843787aa97ed01538bdc2b09a0eb6d2bef5feae22047ddea84ac45439e149d5a347731d4e09a0aa8da1ac1cf03fa641fed1229c9cc1eda153a9210f64b43ce27fc6c71498cdc21d942fa9b80f6fe2592f320ef4c29f5551bc97975ee6784ce0cc9d129c36e7281d77cbaa0c21eca25", + "a6a23569bcf9f300b6bd36bede6b13c8fe30d1e3e41e107d5dc19e5ae3e177bd7838adbd9c0339f34acf3bc2fefbd93603790feca157f2371db133785afa8f3176767e95160438038ec78de44571c85bba20128b763dd69ef53983019bbdd7d6c2f9aae96b8bb0a9c8da1a941180b2c540feba7ec1732bef759ac31c2a92870ab389e4432eee7a31f0d61315755b1dce610333b97cd710786ba2c0f5f682b23eae5cc6463015d0cf8625f8ba7d18aae12854f7469c78962796be17ccca47252ad3b44e72ad6d5f75eaaecf5f9dd89c3cef8155044b0caccb4104c96e682e4141278b3adf659bc9fd1ffd274af00f9fa737d321d91c9263fac12edc6183c176e3a726d83bc676ae2f661b93cd47d3f4d19c3dc1f36c2212bca5af1a7c0b91347b2163bc88ab3f182bcad57c8df8726af7c935ec7431f117fe9b477d14fc6adce8c9588d78f490f0d1e0611081e8c1e01bfec04660186bf1284ac6c99b0bab8fa75d991c5ebe794671b33c39659c9a4dc2fca6de812222e472e0e6b7a22af6521f3fe0793cd64034da703ed63807891a698ac192916e00bed98cbf0bb47ff9ff77bcaa8be2cb3fff028f9e7194e3070000" + ], + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 23:50:13 GMT", + "content-type", + "application/json", + "transfer-encoding", + "chunked", + "connection", + "close", + "cf-ray", + "7593bc8b68dfa724-PHX", + "cache-control", + "no-store", + "content-encoding", + "gzip", + "strict-transport-security", + "max-age=31536000; includeSubDomains", + "vary", + "Accept-Encoding, User-Agent", + "cf-cache-status", + "DYNAMIC", + "pragma", + "no-cache", + "x-correlation-id", + "7593bc8b68dfa724", + "x-ratelimit-1m-limit", + "24000, 24000, 360000", + "x-ratelimit-1m-remaining", + "23994, 23998, 357725", + "x-ratelimit-1m-reset", + "46913, 46912, 46912", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/f_ecom_zzrf_001/oauth2/token" + ], + "responseIsBinary": false + }, + { + "scope": "http://localhost:3000", + "method": "GET", + "path": "/mobify/proxy/api/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions/campaigns/promo_9_promo_43_promo_9_promo_22_promo_38_promo_33_promo_42_promo_11_promo_10_promo_7_promo_18_promo_46_promo_48_promo_42_promo_26_promo_23_promo_1_promo_23_promo_40_promo_44_promo_31_promo_2_promo_2_promo_48_promo_20_promo_5_promo_16_promo_43_promo_49_promo_11_promo_31_promo_36_promo_35_promo_20_promo_39_promo_30_promo_18_promo_22_promo_2_promo_39_promo_33_promo_22_promo_15_promo_31_promo_27_promo_27_promo_15_promo_10_promo_8_promo_5_promo_18?siteId=RefArchGlobal", + "body": "", + "status": 400, + "response": { + "title": "Bad Request", + "type": "https://api.commercecloud.salesforce.com/documentation/error/v1/errors/invalid-uri-parameter", + "detail": "Invalid value 'promo_9_promo_43_promo_9_promo_22_promo_38_promo_33_promo_42_promo_11_promo_10_promo_7_promo_18_promo_46_promo_48_promo_42_promo_26_promo_23_promo_1_promo_23_promo_40_promo_44_promo_31_promo_2_promo_2_promo_48_promo_20_promo_5_promo_16_promo_43_promo_49_promo_11_promo_31_promo_36_promo_35_promo_20_promo_39_promo_30_promo_18_promo_22_promo_2_promo_39_promo_33_promo_22_promo_15_promo_31_promo_27_promo_27_promo_15_promo_10_promo_8_promo_5_promo_18' for uri parameter campaignId. expected maxLength: 256, actual: 448" + }, + "rawHeaders": [ + "date", + "Wed, 12 Oct 2022 23:50:13 GMT", + "content-type", + "application/problem+json; charset=UTF-8", + "content-length", + "681", + "connection", + "close", + "cf-ray", + "7593bc8dd862a6f4-PHX", + "strict-transport-security", + "max-age=31536000; includeSubdomains;", + "cf-cache-status", + "DYNAMIC", + "x-correlation-id", + "7593bc8dd862a6f4", + "Vary", + "Accept-Encoding", + "server", + "cloudflare", + "x-proxy-request-url", + "https://kv7kzm78.api.commercecloud.salesforce.com/pricing/shopper-promotions/v1/organizations/f_ecom_zzrf_001/promotions/campaigns/promo_9_promo_43_promo_9_promo_22_promo_38_promo_33_promo_42_promo_11_promo_10_promo_7_promo_18_promo_46_promo_48_promo_42_promo_26_promo_23_promo_1_promo_23_promo_40_promo_44_promo_31_promo_2_promo_2_promo_48_promo_20_promo_5_promo_16_promo_43_promo_49_promo_11_promo_31_promo_36_promo_35_promo_20_promo_39_promo_30_promo_18_promo_22_promo_2_promo_39_promo_33_promo_22_promo_15_promo_31_promo_27_promo_27_promo_15_promo_10_promo_8_promo_5_promo_18?siteId=RefArchGlobal" + ], + "responseIsBinary": false + } +] \ No newline at end of file diff --git a/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts index 4cb07334fb..ca28125984 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts @@ -31,7 +31,7 @@ function useProducts( function useProducts( arg: UseProductsArg, options?: UseQueryOptions | Response, Error> -) { +): UseQueryResult | Response, Error> { const {headers, rawResponse, ...parameters} = arg return useQuery( ['products', arg], diff --git a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.test.tsx b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.test.tsx new file mode 100644 index 0000000000..21a12388bc --- /dev/null +++ b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.test.tsx @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2022, salesforce.com, inc. + * All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +import React, {ReactElement} from 'react' +import '@testing-library/jest-dom' +import {mockHttpResponses, renderWithProviders} from '../../test-utils' +import {screen, waitFor} from '@testing-library/react' +import {usePromotions, usePromotionsForCampaign} from './query' +import path from 'path' + +const {withMocks} = mockHttpResponses({directory: path.join(__dirname, '../../../mock-responses')}) + +const PromotionsComponent = ({ids}: {ids: string}): ReactElement => { + const {data, isLoading, error} = usePromotions({ + ids + }) + + return ( +
+ {isLoading && Loading...} + {error && error} + {data &&
Total: {data.total}
} + {data && ( +
+ {data.data?.map(({name}) => ( +
{name}
+ ))} +
+ )} +
+ ) +} + +const PromotionsForCampaignComponent = ({campaignId}: {campaignId: string}): ReactElement => { + const {data, isLoading, error} = usePromotionsForCampaign({ + campaignId + }) + return ( +
+ {isLoading && Loading...} + {error && error} + {data &&
Total: {data.total}
} + {data && ( +
+ {data.data?.map(({name}) => ( +
{name}
+ ))} +
+ )} +
+ ) +} + +const tests = [ + { + hook: 'usePromotions', + cases: [ + { + name: 'returns data', + assertions: withMocks(async () => { + const ids = '10offsuits,50%offorder' + renderWithProviders() + const promotionNames = ["10% off men's suits", '50% off order'] + expect(screen.queryByText(promotionNames[0])).toBeNull() + expect(screen.queryByText(promotionNames[1])).toBeNull() + expect(screen.getByText('Loading...')).toBeInTheDocument() + await waitFor(() => screen.getByText(promotionNames[0])) + + expect(screen.queryByText(promotionNames[0])).toBeInTheDocument() + expect(screen.queryByText(promotionNames[1])).toBeInTheDocument() + expect(screen.getByText(`Total: ${promotionNames.length}`)).toBeInTheDocument() + }) + }, + { + name: 'returns error', + assertions: withMocks(async () => { + // Maximum characters is 256, generating 51 ids here will exceed that limit + // and cause the server to return an error + const fakeIds = [...new Array(51)] + .map((i) => `promo_${Math.floor(Math.random() * 50)}`) + .join(',') + renderWithProviders() + + expect(screen.getByText('Loading...')).toBeInTheDocument() + await waitFor(() => screen.getByText('error')) + expect(screen.getByText('error')).toBeInTheDocument() + expect(screen.queryByText('Loading...')).toBeNull() + }) + } + ] + }, + { + hook: 'usePromotionsForCampaign', + cases: [ + { + name: 'returns data', + assertions: withMocks(async () => { + const ids = '10offsuits,50%offorder' + renderWithProviders( + + ) + const promotionNames = [ + '50% off Shipping Amount Above 100', + "10% off men's suits", + '50% off order' + ] + expect(screen.queryByText(promotionNames[0])).toBeNull() + expect(screen.queryByText(promotionNames[1])).toBeNull() + expect(screen.getByText('Loading...')).toBeInTheDocument() + await waitFor(() => screen.getByText(promotionNames[0])) + + expect(screen.queryByText(promotionNames[0])).toBeInTheDocument() + expect(screen.queryByText(promotionNames[1])).toBeInTheDocument() + expect(screen.getByText(`Total: ${promotionNames.length}`)).toBeInTheDocument() + }) + }, + { + name: 'returns error', + assertions: withMocks(async () => { + // passing invalid string exceeded 356 char should cause the server to return an error + const invalidId = [...new Array(51)] + .map((i) => `promo_${Math.floor(Math.random() * 50)}`) + .join('_') + renderWithProviders() + + expect(screen.getByText('Loading...')).toBeInTheDocument() + await waitFor(() => screen.getByText('error')) + expect(screen.getByText('error')).toBeInTheDocument() + expect(screen.queryByText('Loading...')).toBeNull() + }) + } + ] + } +] + +tests.forEach(({hook, cases}) => { + describe(hook, () => { + afterEach(() => { + jest.resetAllMocks() + }) + cases.forEach(({name, assertions}) => { + test(name, assertions) + }) + }) +}) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts index 13856ed659..14776a238a 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts @@ -6,11 +6,16 @@ */ import {ApiClients, Argument, DataType} from '../types' import {useQuery} from '../useQuery' -import useCommerceApi from '../useCommerceApi' -import {UseQueryResult} from '@tanstack/react-query' +import {UseQueryOptions, UseQueryResult} from '@tanstack/react-query' type Client = ApiClients['shopperPromotions'] +type UsePromotionsParameters = NonNullable>['parameters'] +type UsePromotionsHeaders = NonNullable>['headers'] +type UsePromotionsArg = { + headers?: UsePromotionsHeaders + rawResponse?: boolean +} & UsePromotionsParameters /** * A hook for `ShopperPromotions#getPromotions`. * Returns an array of enabled promotions for a list of specified IDs. In the request URL, you can specify up to 50 IDs. If you specify an ID that contains either parentheses or the separator characters, you must URL encode these characters. Each request returns only enabled promotions as the server does not consider promotion qualifiers or schedules. @@ -18,12 +23,38 @@ type Client = ApiClients['shopperPromotions'] * @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperpromotions.shopperpromotions-1.html#getpromotions} for more information on the parameters and returned data type. * @returns An object describing the state of the request. */ -export const usePromotions = ( - arg: Argument -): UseQueryResult, Error> => { - const {shopperPromotions: client} = useCommerceApi() - return useQuery(['promotions', arg], () => client.getPromotions(arg)) +function usePromotions( + arg: Omit & {rawResponse?: false}, + options?: UseQueryOptions | Response, Error> +): UseQueryResult, Error> +function usePromotions( + arg: Omit & {rawResponse: true}, + options?: UseQueryOptions | Response, Error> +): UseQueryResult +function usePromotions( + arg: UsePromotionsArg, + options?: UseQueryOptions | Response, Error> +): UseQueryResult | Response, Error> { + const {headers, rawResponse, ...parameters} = arg + return useQuery( + ['promotions', arg], + (_, {shopperPromotions}) => { + return shopperPromotions.getPromotions({parameters, headers}, rawResponse) + }, + options + ) } + +type UsePromotionsForCampaignParameters = NonNullable< + Argument +>['parameters'] +type UsePromotionsForCampaignHeaders = NonNullable< + Argument +>['headers'] +type UsePromotionsForCampaignArg = { + headers?: UsePromotionsForCampaignHeaders + rawResponse?: boolean +} & UsePromotionsForCampaignParameters /** * A hook for `ShopperPromotions#getPromotionsForCampaign`. * Handles get promotion by filter criteria. Returns an array of enabled promotions matching the specified filter @@ -35,9 +66,25 @@ promotions, since the server does not consider promotion qualifiers or schedules * @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperpromotions.shopperpromotions-1.html#getpromotionsforcampaign} for more information on the parameters and returned data type. * @returns An object describing the state of the request. */ -export const usePromotionsForCampaign = ( - arg: Argument -): UseQueryResult, Error> => { - const {shopperPromotions: client} = useCommerceApi() - return useQuery(['promotions-for-campaign', arg], () => client.getPromotionsForCampaign(arg)) +function usePromotionsForCampaign( + arg: Omit & {rawResponse?: false}, + options?: UseQueryOptions | Response, Error> +): UseQueryResult, Error> +function usePromotionsForCampaign( + arg: Omit & {rawResponse: true}, + options?: UseQueryOptions | Response, Error> +): UseQueryResult +function usePromotionsForCampaign( + arg: UsePromotionsForCampaignArg, + options?: UseQueryOptions | Response, Error> +): UseQueryResult | Response, Error> { + const {headers, rawResponse, ...parameters} = arg + return useQuery( + ['promotions-for-campaign', arg], + (_, {shopperPromotions}) => + shopperPromotions.getPromotionsForCampaign({parameters, headers}, rawResponse), + options + ) } + +export {usePromotions, usePromotionsForCampaign} diff --git a/packages/test-commerce-sdk-react/app/pages/home.tsx b/packages/test-commerce-sdk-react/app/pages/home.tsx index cb44b2541d..9fc0b01efa 100644 --- a/packages/test-commerce-sdk-react/app/pages/home.tsx +++ b/packages/test-commerce-sdk-react/app/pages/home.tsx @@ -15,7 +15,7 @@ const Home = () => {

Feel free to use this playground to develop commerce-sdk-react.

If you are adding new features to the package, please add a new page on this app to - demostrate the feature. + demonstrate the feature.

Happy coding.

@@ -33,6 +33,12 @@ const Home = () => {
  • useSearchSuggestions
  • +
  • + usePromotions +
  • +
  • + usePromotionsForCampaign +
  • useShopperLoginHelper
  • diff --git a/packages/test-commerce-sdk-react/app/pages/use-promotions-for-campaign.tsx b/packages/test-commerce-sdk-react/app/pages/use-promotions-for-campaign.tsx new file mode 100644 index 0000000000..62bdb8e9d8 --- /dev/null +++ b/packages/test-commerce-sdk-react/app/pages/use-promotions-for-campaign.tsx @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022, salesforce.com, inc. + * All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +import React from 'react' +import {usePromotionsForCampaign} from 'commerce-sdk-react' +import Json from '../components/Json' + +const UsePromotionsForCampaign = () => { + // campaign id need to be encoded before sent off, it could have special char. + // e.g "50% off order" + const campaignId = encodeURI('promotion-campaign') + const {data: result, isLoading, error} = usePromotionsForCampaign({ + campaignId + }) + if (isLoading) { + return ( +
    +

    Promotions for a Campaign

    +

    Loading...

    +
    + ) + } + + if (error) { + return

    Something is wrong

    + } + + return ( + <> +

    Promotions for a Campaign

    + {result?.data?.map(({id, name}) => { + return ( +
    +
    Name: {name}
    +
    Id: {id}
    +
    + ) + })} +
    +
    +
    Returning data
    + +
    + + ) +} + +export default UsePromotionsForCampaign diff --git a/packages/test-commerce-sdk-react/app/pages/use-promotions.tsx b/packages/test-commerce-sdk-react/app/pages/use-promotions.tsx new file mode 100644 index 0000000000..f1e07f9cf9 --- /dev/null +++ b/packages/test-commerce-sdk-react/app/pages/use-promotions.tsx @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022, salesforce.com, inc. + * All rights reserved. + * SPDX-License-Identifier: BSD-3-Clause + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +import React from 'react' +import {usePromotions} from 'commerce-sdk-react' +import Json from '../components/Json' + +function UsePromotions() { + const {data: result, isLoading, error} = usePromotions({ids: '10offsuits,50%offorder'}) + if (isLoading) { + return ( +
    +

    Promotions

    +

    Loading...

    +
    + ) + } + if (error) { + return

    Something is wrong

    + } + return ( + <> +

    Promotions:

    + {result?.data?.map(({id, name}) => { + return ( +
    +
    Name: {name}
    +
    id: {id}
    +
    + ) + })} +
    +

    Returning data

    + + + ) +} + +export default UsePromotions diff --git a/packages/test-commerce-sdk-react/app/routes.tsx b/packages/test-commerce-sdk-react/app/routes.tsx index 9db2eb4a49..a016493465 100644 --- a/packages/test-commerce-sdk-react/app/routes.tsx +++ b/packages/test-commerce-sdk-react/app/routes.tsx @@ -13,6 +13,8 @@ const UseProduct = loadable(() => import('./pages/use-shopper-product')) const UseCategories = loadable(() => import('./pages/use-shopper-categories')) const UseCategory = loadable(() => import('./pages/use-shopper-category')) const UseProductSearch = loadable(() => import('./pages/use-product-search')) +const UsePromotions = loadable(() => import('./pages/use-promotions')) +const UsePromotionsForCampaign = loadable(() => import('./pages/use-promotions-for-campaign')) const UseShopperLoginHelper = loadable(() => import('./pages/use-shopper-login-helper')) const UseShopperBaskets = loadable(() => import('./pages/use-shopper-baskets')) const QueryErrors = loadable(() => import('./pages/query-errors')) @@ -47,6 +49,14 @@ const routes = [ path: '/search-suggestions', component: UseSearchSuggestions }, + { + path: '/use-promotions', + component: UsePromotions + }, + { + path: '/use-promotions-for-campaign', + component: UsePromotionsForCampaign + }, { path: '/slas-helpers', component: UseShopperLoginHelper