Skip to content

Commit 7da3c8f

Browse files
authored
Merge pull request #2815 from ORCID/lmendoza/PD-5332-v2
PD-5332
2 parents b27f02a + 351f0a3 commit 7da3c8f

4 files changed

Lines changed: 215 additions & 161 deletions

File tree

src/app/core/wordpress/wordpress.service.spec.ts

Lines changed: 71 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,165 +4,139 @@ import {
44
HttpClientTestingModule,
55
HttpTestingController,
66
} from '@angular/common/http/testing'
7-
import { RouterTestingModule } from '@angular/router/testing'
8-
import { PlatformInfoService } from 'src/app/cdk/platform-info'
9-
import { WINDOW_PROVIDERS } from 'src/app/cdk/window'
107
import { LOCALE_ID } from '@angular/core'
118

12-
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'
13-
149
describe('WordpressService', () => {
1510
let service: WordpressService
1611
let httpMock: HttpTestingController
12+
let originalRuntimeEnvironment: unknown
13+
14+
const WORDPRESS_S3 = 'https://homepage-qa.orcid.org'
15+
const WORDPRESS_S3_FALLBACK = 'https://homepage-fallback.orcid.org'
16+
const primaryIndexUrl = `${WORDPRESS_S3}/index.html`
17+
const fallbackIndexUrl = `${WORDPRESS_S3_FALLBACK}/index.html`
1718

1819
beforeEach(() => {
20+
originalRuntimeEnvironment = (globalThis as any).runtimeEnvironment
21+
;(globalThis as any).runtimeEnvironment = {
22+
...(originalRuntimeEnvironment as Record<string, unknown> | undefined),
23+
WORDPRESS_S3,
24+
WORDPRESS_S3_FALLBACK,
25+
}
26+
1927
TestBed.configureTestingModule({
20-
imports: [HttpClientTestingModule, RouterTestingModule],
21-
providers: [
22-
WINDOW_PROVIDERS,
23-
PlatformInfoService,
24-
{ provide: LOCALE_ID, useValue: 'en' },
25-
],
28+
imports: [HttpClientTestingModule],
29+
providers: [{ provide: LOCALE_ID, useValue: 'en' }],
2630
})
2731
service = TestBed.inject(WordpressService)
2832
httpMock = TestBed.inject(HttpTestingController)
2933
})
3034

3135
afterEach(() => {
3236
httpMock.verify()
37+
;(globalThis as any).runtimeEnvironment = originalRuntimeEnvironment
3338
})
3439

3540
it('should be created', () => {
3641
expect(service).toBeTruthy()
3742
})
3843

39-
it('should fetch home page post from primary URL', () => {
40-
const mockHtml = '<html></html>'
44+
it('fetches home page post from primary URL', () => {
45+
const mockHtml =
46+
'<html><head></head><body><img src="./assets/image.png"></body></html>'
47+
4148
service.getHomePagePost().subscribe((html) => {
42-
expect(html).toContain(mockHtml)
49+
expect(html).toContain(`${WORDPRESS_S3}/assets/image.png`)
4350
})
4451

45-
const req = httpMock.expectOne(
46-
`${runtimeEnvironment.WORDPRESS_S3}/index.html`
47-
)
52+
const req = httpMock.expectOne(primaryIndexUrl)
4853
expect(req.request.method).toBe('GET')
4954
req.flush(mockHtml)
5055
})
5156

52-
it('should fetch home page post from fallback URL when primary fails', () => {
57+
it('fetches home page post from fallback URL when primary fails', () => {
5358
const mockHtml = '<html></html>'
59+
5460
service.getHomePagePost().subscribe((html) => {
5561
expect(html).toContain(mockHtml)
5662
})
5763

58-
const primaryReq = httpMock.expectOne(
59-
`${runtimeEnvironment.WORDPRESS_S3}/index.html`
60-
)
64+
const primaryReq = httpMock.expectOne(primaryIndexUrl)
6165
primaryReq.flush(null, { status: 500, statusText: 'Server Error' })
6266

63-
const fallbackReq = httpMock.expectOne(
64-
`${runtimeEnvironment.WORDPRESS_S3_FALLBACK}/index.html`
65-
)
67+
const fallbackReq = httpMock.expectOne(fallbackIndexUrl)
6668
expect(fallbackReq.request.method).toBe('GET')
6769
fallbackReq.flush(mockHtml)
6870
})
6971

70-
it('should fetch home page CSS from primary URL', () => {
71-
const mockCss = 'body { margin: 0; }'
72+
it('fetches home page CSS from the stylesheet declared in index.html', () => {
73+
const mockIndex =
74+
'<html><head><link href="./wordpress-homepage-d4235c1c61.css" rel="stylesheet" /></head><body></body></html>'
75+
const mockCss = '.hero{background:url("assets/bg.png")}'
76+
7277
service.getHomePageCSS().subscribe((css) => {
73-
expect(css).toContain(mockCss)
78+
expect(css).toContain(`${WORDPRESS_S3}/assets/bg.png`)
7479
})
7580

76-
const req = httpMock.expectOne(
77-
`${runtimeEnvironment.WORDPRESS_S3}/wordpress-homepage.css`
81+
const indexReq = httpMock.expectOne(primaryIndexUrl)
82+
indexReq.flush(mockIndex)
83+
const cssReq = httpMock.expectOne(
84+
`${WORDPRESS_S3}/wordpress-homepage-d4235c1c61.css`
7885
)
79-
expect(req.request.method).toBe('GET')
80-
req.flush(mockCss)
86+
cssReq.flush(mockCss)
8187
})
8288

83-
it('should fetch home page CSS from fallback URL when primary fails', () => {
84-
const mockCss = 'body { margin: 0; }'
89+
it('falls back to legacy CSS path if index.html has no stylesheet', () => {
90+
const mockIndex = '<html><head></head><body></body></html>'
91+
const mockCss = 'body{margin:0}'
92+
8593
service.getHomePageCSS().subscribe((css) => {
8694
expect(css).toContain(mockCss)
8795
})
8896

89-
const primaryReq = httpMock.expectOne(
90-
`${runtimeEnvironment.WORDPRESS_S3}/wordpress-homepage.css`
91-
)
92-
primaryReq.flush(null, { status: 500, statusText: 'Server Error' })
93-
94-
const fallbackReq = httpMock.expectOne(
95-
`${runtimeEnvironment.WORDPRESS_S3_FALLBACK}/wordpress-homepage.css`
97+
const indexReq = httpMock.expectOne(primaryIndexUrl)
98+
indexReq.flush(mockIndex)
99+
const cssReq = httpMock.expectOne(
100+
`${WORDPRESS_S3}/wordpress-homepage.css`
96101
)
97-
expect(fallbackReq.request.method).toBe('GET')
98-
fallbackReq.flush(mockCss)
102+
cssReq.flush(mockCss)
99103
})
100104

101-
it('should fetch home page JS from primary URL', () => {
102-
const mockJs = 'console.log("Hello, World!");'
103-
service.getHomePageJS().subscribe((js) => {
104-
expect(js).toContain(mockJs)
105-
})
105+
it('fetches home page JS from script declared in index.html', () => {
106+
const mockIndex =
107+
'<html><head><script defer src="./wordpress-homepage-64bd37a0a7.js"></script></head><body></body></html>'
108+
const mockJs = 'const image = "./assets/test.png"'
106109

107-
const req = httpMock.expectOne(
108-
`${runtimeEnvironment.WORDPRESS_S3}/wordpress-homepage.js`
109-
)
110-
expect(req.request.method).toBe('GET')
111-
req.flush(mockJs)
112-
})
113-
114-
it('should fetch home page JS from fallback URL when primary fails', () => {
115-
const mockJs = 'console.log("Hello, World!");'
116110
service.getHomePageJS().subscribe((js) => {
117-
expect(js).toContain(mockJs)
118-
})
119-
120-
const primaryReq = httpMock.expectOne(
121-
`${runtimeEnvironment.WORDPRESS_S3}/wordpress-homepage.js`
122-
)
123-
primaryReq.flush(null, { status: 500, statusText: 'Server Error' })
124-
125-
const fallbackReq = httpMock.expectOne(
126-
`${runtimeEnvironment.WORDPRESS_S3_FALLBACK}/wordpress-homepage.js`
127-
)
128-
expect(fallbackReq.request.method).toBe('GET')
129-
fallbackReq.flush(mockJs)
130-
})
131-
132-
it('should fetch home page post from primary URL and update asset paths', () => {
133-
const mockHtml =
134-
'<html><head></head><body><img src="./assets/image.png"></body></html>'
135-
const expectedHtml = `<html><head></head><body><img src="${runtimeEnvironment.WORDPRESS_S3}/assets/image.png"></body></html>`
136-
137-
service.getHomePagePost().subscribe((html) => {
138-
expect(html).toBe(expectedHtml)
111+
expect(js).toContain(
112+
`const image = "${WORDPRESS_S3}/assets/test.png"`
113+
)
139114
})
140115

141-
const req = httpMock.expectOne(
142-
`${runtimeEnvironment.WORDPRESS_S3}/index.html`
116+
const indexReq = httpMock.expectOne(primaryIndexUrl)
117+
indexReq.flush(mockIndex)
118+
const jsReq = httpMock.expectOne(
119+
`${WORDPRESS_S3}/wordpress-homepage-64bd37a0a7.js`
143120
)
144-
expect(req.request.method).toBe('GET')
145-
req.flush(mockHtml)
121+
jsReq.flush(mockJs)
146122
})
147123

148-
it('should fetch home page post from fallback URL and update asset paths when primary fails', () => {
149-
const mockHtml =
150-
'<html><head></head><body><img src="./assets/image.png"></body></html>'
151-
const expectedHtml = `<html><head></head><body><img src="${runtimeEnvironment.WORDPRESS_S3_FALLBACK}/assets/image.png"></body></html>`
124+
it('fetches module JS from script declared in index.html', () => {
125+
const mockIndex =
126+
'<html><head><script type="module" src="./wordpress-homepage-modules-34238353bb.js"></script></head><body></body></html>'
127+
const mockJs = 'const icon = "./assets/icon.svg"'
152128

153-
service.getHomePagePost().subscribe((html) => {
154-
expect(html).toBe(expectedHtml)
129+
service.getHomePageModulesJS().subscribe((js) => {
130+
expect(js).toContain(
131+
`const icon = "${WORDPRESS_S3}/assets/icon.svg"`
132+
)
155133
})
156134

157-
const primaryReq = httpMock.expectOne(
158-
`${runtimeEnvironment.WORDPRESS_S3}/index.html`
159-
)
160-
primaryReq.flush(null, { status: 500, statusText: 'Server Error' })
161-
162-
const fallbackReq = httpMock.expectOne(
163-
`${runtimeEnvironment.WORDPRESS_S3_FALLBACK}/index.html`
135+
const indexReq = httpMock.expectOne(primaryIndexUrl)
136+
indexReq.flush(mockIndex)
137+
const jsReq = httpMock.expectOne(
138+
`${WORDPRESS_S3}/wordpress-homepage-modules-34238353bb.js`
164139
)
165-
expect(fallbackReq.request.method).toBe('GET')
166-
fallbackReq.flush(mockHtml)
140+
jsReq.flush(mockJs)
167141
})
168142
})

0 commit comments

Comments
 (0)