@codecademy/brand 2.4.0 → 2.4.1-alpha.071c85492c.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,7 @@ export function RecurlyProvider({
16
16
  }) {
17
17
  const [recurly, setRecurly] = useState(undefined);
18
18
  useEffect(() => {
19
- loadRecurly(config).then(setRecurly);
19
+ loadRecurly(config)?.then(setRecurly);
20
20
  // Exclude `config` dependency as it should be treated as static.
21
21
  // eslint-disable-next-line react-hooks/exhaustive-deps
22
22
  }, []);
@@ -3,4 +3,4 @@ export type RecurlyConfig = {
3
3
  publicKey: string;
4
4
  required: string[];
5
5
  };
6
- export declare function loadRecurly(config: RecurlyConfig): Promise<Recurly>;
6
+ export declare function loadRecurly(config: RecurlyConfig): Promise<Recurly> | undefined;
@@ -1,6 +1,6 @@
1
1
  import { w } from './w';
2
2
  export function loadRecurly(config) {
3
- if (!w._recurlyPromise) {
3
+ if (w && w._recurlyPromise) {
4
4
  w._recurlyPromise = new Promise(resolve => {
5
5
  const script = w.document.createElement('script');
6
6
  script.src = 'https://js.recurly.com/v4/recurly.js';
@@ -18,5 +18,5 @@ export function loadRecurly(config) {
18
18
  w.document.head.appendChild(script);
19
19
  });
20
20
  }
21
- return w._recurlyPromise;
21
+ return w?._recurlyPromise;
22
22
  }
@@ -10,5 +10,5 @@ export type W = {
10
10
  _recurlyPromise?: Promise<Recurly>;
11
11
  document: Doc;
12
12
  };
13
- export declare const w: W;
13
+ export declare const w: W | undefined;
14
14
  export {};
@@ -1,4 +1,4 @@
1
1
  // By importing/exporting w as a window object, we can easily mock it in tests.
2
2
 
3
3
  // Exported for easy mocking
4
- export const w = window;
4
+ export const w = typeof window === 'undefined' ? undefined : window;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/brand",
3
3
  "description": "Brand component library for Codecademy",
4
- "version": "2.4.0",
4
+ "version": "2.4.1-alpha.071c85492c.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "dependencies": {
7
7
  "@emotion/is-prop-valid": "^1.2.1",
@@ -1,88 +0,0 @@
1
- import { setupRtl } from '@codecademy/gamut-tests';
2
- import { RecurlyProvider } from '../recurly';
3
- import { Elements, useElements } from '.';
4
- import { jsx as _jsx } from "react/jsx-runtime";
5
- let mockRecurlyPromise;
6
- jest.mock('../recurly/loadRecurly', () => ({
7
- loadRecurly: () => mockRecurlyPromise
8
- }));
9
- const renderView = setupRtl(({
10
- children
11
- }) => children);
12
- const jsEventLoop = () => new Promise(r => setTimeout(r, 0));
13
- describe('Elements', () => {
14
- it('should show content before recurly loads', () => {
15
- mockRecurlyPromise = new Promise(() => {
16
- // never resolve
17
- });
18
- const {
19
- view
20
- } = renderView({
21
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
22
- publicKey: "testKey",
23
- required: ['cvv', 'postal_code'],
24
- children: /*#__PURE__*/_jsx(Elements, {
25
- children: "test-content"
26
- })
27
- })
28
- });
29
- expect(view.getByText('test-content')).toBeInTheDocument();
30
- });
31
- it('should call Elements on recurly', async () => {
32
- const mockRecurly = {
33
- Elements: jest.fn()
34
- };
35
- mockRecurlyPromise = Promise.resolve(mockRecurly);
36
- renderView({
37
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
38
- publicKey: "testKey",
39
- required: ['cvv', 'postal_code'],
40
- children: /*#__PURE__*/_jsx(Elements, {})
41
- })
42
- });
43
- await jsEventLoop(); // one for the Provider's useEffect to run
44
- await jsEventLoop(); // one for the subsequent re-render where useMemo runs
45
- expect(mockRecurly.Elements).toHaveBeenCalled();
46
- });
47
- });
48
- describe('useElements', () => {
49
- const Consumer = () => {
50
- try {
51
- const elements = useElements();
52
- return /*#__PURE__*/_jsx("div", {
53
- children: typeof elements
54
- });
55
- } catch {
56
- return /*#__PURE__*/_jsx("div", {
57
- children: "error"
58
- });
59
- }
60
- };
61
- it('throws an error when used without a Elements provider', () => {
62
- const {
63
- view
64
- } = renderView({
65
- children: /*#__PURE__*/_jsx(Consumer, {})
66
- });
67
- expect(view.getByText('error')).toBeInTheDocument();
68
- });
69
- it('returns null initially, then an Elements instance once Recurly loads', async () => {
70
- const mockRecurly = {
71
- Elements: () => ({})
72
- };
73
- mockRecurlyPromise = Promise.resolve(mockRecurly);
74
- const {
75
- view
76
- } = renderView({
77
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
78
- publicKey: "testKey",
79
- required: ['cvv', 'postal_code'],
80
- children: /*#__PURE__*/_jsx(Elements, {
81
- children: /*#__PURE__*/_jsx(Consumer, {})
82
- })
83
- })
84
- });
85
- expect(view.getByText('undefined')).toBeInTheDocument();
86
- expect(await view.findByText('object')).toBeInTheDocument();
87
- });
88
- });
@@ -1,63 +0,0 @@
1
- import { setupRtl } from '@codecademy/gamut-tests';
2
- import { RecurlyProvider, useRecurly } from '.';
3
- import { jsx as _jsx } from "react/jsx-runtime";
4
- let mockRecurlyPromise;
5
- jest.mock('./loadRecurly', () => ({
6
- loadRecurly: () => mockRecurlyPromise
7
- }));
8
- describe('RecurlyProvider', () => {
9
- const renderView = setupRtl(RecurlyProvider);
10
- it('should show content before recurly loads', () => {
11
- mockRecurlyPromise = new Promise(() => {
12
- // never resolve
13
- });
14
- const {
15
- view
16
- } = renderView({
17
- publicKey: 'testKey',
18
- required: ['cvv', 'postal_code'],
19
- children: 'test-content'
20
- });
21
- expect(view.getByText('test-content')).toBeInTheDocument();
22
- });
23
- });
24
- describe('useRecurly', () => {
25
- const renderView = setupRtl(({
26
- children
27
- }) => children);
28
- const Consumer = () => {
29
- try {
30
- const recurly = useRecurly();
31
- return /*#__PURE__*/_jsx("div", {
32
- children: typeof recurly
33
- });
34
- } catch {
35
- return /*#__PURE__*/_jsx("div", {
36
- children: "error"
37
- });
38
- }
39
- };
40
- it('throws an error when used without a RecurlyProvider', () => {
41
- mockRecurlyPromise = Promise.resolve({});
42
- const {
43
- view
44
- } = renderView({
45
- children: /*#__PURE__*/_jsx(Consumer, {})
46
- });
47
- expect(view.getByText('error')).toBeInTheDocument();
48
- });
49
- it('returns undefined and then an instance when used inside a RecurlyProvider', async () => {
50
- mockRecurlyPromise = Promise.resolve({});
51
- const {
52
- view
53
- } = renderView({
54
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
55
- publicKey: "testKey",
56
- required: ['cvv', 'postal_code'],
57
- children: /*#__PURE__*/_jsx(Consumer, {})
58
- })
59
- });
60
- expect(view.getByText('undefined')).toBeInTheDocument();
61
- expect(await view.findByText('object')).toBeInTheDocument();
62
- });
63
- });
@@ -1,57 +0,0 @@
1
- import { loadRecurly } from './loadRecurly';
2
- const w = {};
3
- jest.mock('./w', () => ({
4
- w
5
- }));
6
- const mockConfig = {
7
- publicKey: 'test-public-key',
8
- required: ['field1', 'field2']
9
- };
10
- describe('loadRecurly', () => {
11
- let mockScript;
12
- let mockRecurly;
13
- beforeEach(() => {
14
- mockScript = {
15
- src: '',
16
- async: false,
17
- onload: null
18
- };
19
- mockRecurly = {
20
- configure: jest.fn()
21
- };
22
- w.recurly = undefined;
23
- w._recurlyPromise = undefined;
24
- w.document = {
25
- createElement: jest.fn().mockReturnValue(mockScript),
26
- head: {
27
- appendChild: jest.fn()
28
- }
29
- };
30
- });
31
- it('should create a script, load recurly, and configure it', async () => {
32
- const promise = loadRecurly(mockConfig);
33
- expect(w.document.createElement).toHaveBeenCalledWith('script');
34
- expect(mockScript.src).toBe('https://js.recurly.com/v4/recurly.js');
35
- expect(mockScript.async).toBe(true);
36
- expect(w.document.head.appendChild).toHaveBeenCalledWith(mockScript);
37
- expect(mockScript.onload).toBeDefined();
38
- w.recurly = mockRecurly;
39
- mockScript.onload?.(new Event('load'));
40
- await promise;
41
- expect(mockRecurly.configure).toHaveBeenCalledWith(mockConfig);
42
- });
43
- it('should only create one script element when called multiple times', async () => {
44
- const promises = [loadRecurly(mockConfig), loadRecurly(mockConfig), loadRecurly(mockConfig)];
45
- w.recurly = mockRecurly;
46
- mockScript.onload?.(new Event('load'));
47
-
48
- // Should always return the same promise instance
49
- expect(promises[0]).toBe(promises[1]);
50
- expect(promises[1]).toBe(promises[2]);
51
- await Promise.all(promises);
52
-
53
- // Should only create one script element
54
- expect(w.document.createElement).toHaveBeenCalledTimes(1);
55
- expect(w.document.head.appendChild).toHaveBeenCalledTimes(1);
56
- });
57
- });