Skip to content

Commit 72264f5

Browse files
authored
Migrate js binding to watch methods (#190)
* Dependencies update * Extend configuration by adding custom fields configuration
1 parent 7d58a77 commit 72264f5

File tree

20 files changed

+10485
-4906
lines changed

20 files changed

+10485
-4906
lines changed

cypress/integration/01_checkout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const suite = {
2020
};
2121

2222
describe("Checkout", () => {
23-
before(() => {
23+
beforeEach(() => {
2424
// Add product and visit checkout
2525
cy.visit("/index.php?route=product/product&product_id=43");
2626
cy.get("button").contains("Add to Cart").click();

cypress/support/suite.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ interface Suite {
77
address: Address;
88
}
99

10+
const normalizeCity = (city: string): string => {
11+
const lowerCase = city.toLowerCase();
12+
return lowerCase.charAt(0).toUpperCase() + lowerCase.slice(1);
13+
}
14+
1015
const assertions = (
1116
scope: JQuery<HTMLElement>,
1217
selectors: Selectors,
@@ -29,7 +34,7 @@ const assertions = (
2934
cy
3035
.get(selectors.organisation)
3136
.should("have.value", address.organisation_name);
32-
cy.get(selectors.post_town).should("have.value", address.post_town);
37+
cy.get(selectors.post_town).should("have.value", normalizeCity(address.post_town));
3338
cy.get(selectors.country).should("have.value", "257");
3439
cy.get(selectors.postcode).should("have.value", address.postcode);
3540
};
@@ -64,16 +69,16 @@ export const postcodeLookupSuite = (suite: Suite) => {
6469
it("Postcode Lookup", () => {
6570
cy.get(scope).within((scope) => {
6671
cy.get(selectors.country).select("222");
67-
cy.get("#idpc_input")
72+
cy.get(".idpc_lookup input.form-control")
6873
.clear({
6974
force: true,
7075
})
7176
.type(address.postcode, {
7277
force: true,
7378
});
74-
cy.get("#idpc_button").click({ force: true });
79+
cy.get(".idpc-button").click({ force: true });
7580
cy.wait(1000);
76-
cy.get("#idpc_dropdown").select("0");
81+
cy.get(".idpc-select-container select").select("0");
7782
assertions(scope, selectors, address);
7883
});
7984
});

lib/account.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setupBind, Binding, Config } from "@ideal-postcodes/jsutil";
1+
import { Config } from "@ideal-postcodes/jsutil";
22
import { selectors as billingSelectors } from "./billing";
33
import { setupAutocomplete, setupShippingPostcodeLookup } from "./extension";
44

@@ -7,15 +7,13 @@ const selectors = {
77
line_1: "#input-address-1",
88
};
99

10-
const bind = (config: Config) => {
11-
setupBind({
12-
selectors,
13-
}).forEach(({ targets }) => {
14-
setupAutocomplete(config, targets);
15-
setupShippingPostcodeLookup(config, targets);
16-
});
17-
};
10+
const getScope = undefined;
1811

1912
export const pageTest = () => window.location.href.includes("/address");
2013

21-
export const bindings: Binding = { bind, pageTest };
14+
const bind = (config: Config) => {
15+
setupAutocomplete(config, selectors, pageTest, getScope);
16+
setupShippingPostcodeLookup(config, selectors, pageTest, getScope);
17+
};
18+
19+
export const bindings = { bind };

lib/billing.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setupBind, Config } from "@ideal-postcodes/jsutil";
1+
import { getParent, Config } from "@ideal-postcodes/jsutil";
22

33
import { setupAutocomplete, setupPostcodeLookup } from "./extension";
44

@@ -7,22 +7,18 @@ export const selectors = {
77
line_2: '[name="address_2"]',
88
postcode: '[name="postcode"]',
99
post_town: '[name="city"]',
10-
organisation: '[name="company"]',
10+
organisation_name: '[name="company"]',
1111
county: '[name="zone_id"]',
1212
country: '[name="country_id"]',
1313
};
1414

15-
const bind = (config: Config) => {
16-
setupBind({
17-
selectors,
18-
parentScope: "fieldset",
19-
parentTest: (e) => e.id === "address",
20-
}).forEach(({ targets }) => {
21-
setupAutocomplete(config, targets);
22-
setupPostcodeLookup(config, targets);
23-
});
24-
};
15+
const getScope = (anchor: any) => getParent(anchor, "fieldset", (e) => e.id === "address");
2516

2617
export const pageTest = () => window.location.href.includes("/checkout");
2718

28-
export const bindings = { bind, pageTest };
19+
const bind = (config: Config) => {
20+
setupAutocomplete(config, selectors, pageTest, getScope);
21+
setupPostcodeLookup(config, selectors, pageTest, getScope);
22+
};
23+
24+
export const bindings = { bind };

lib/custom.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Config, OutputFields } from "@ideal-postcodes/jsutil";
2+
3+
import { setupAutocomplete, setupPostcodeLookup } from "./extension";
4+
5+
export const pageTest = () => true;
6+
7+
interface CustomConfig extends Config {
8+
customFields?: OutputFields[]
9+
}
10+
11+
const bind = (config: CustomConfig) => {
12+
const selectors = config.customFields || [];
13+
selectors.forEach((selectors: OutputFields) => {
14+
setupAutocomplete(config, selectors, pageTest);
15+
setupPostcodeLookup(config, selectors, pageTest);
16+
});
17+
};
18+
19+
export const bindings = { bind };

0 commit comments

Comments
 (0)