1
1
import React from 'react' ;
2
2
import { expect } from 'chai' ;
3
3
import { shallow } from 'enzyme' ;
4
+ import {
5
+ composeComponentEndpoint ,
6
+ FEaaSComponent ,
7
+ FEaaSComponentParams ,
8
+ FEaaSComponentProps ,
9
+ } from './FEaaSComponent' ;
4
10
import { ComponentFields } from '@sitecore-jss/sitecore-jss/layout' ;
5
- import { FEaaSComponent , FEaaSComponentParams } from './FEaaSComponent' ;
6
11
7
12
describe ( '<FEaaSComponent />' , ( ) => {
8
13
const requiredParams : FEaaSComponentParams = {
9
14
LibraryId : 'library123' ,
10
15
ComponentId : 'component123' ,
11
16
ComponentVersion : 'version123' ,
12
- ComponentRevision : 'revision123 ' ,
17
+ ComponentRevision : 'staged ' ,
13
18
ComponentHostName : 'host123' ,
14
19
} ;
15
20
16
- it ( 'should not render if missing params' , ( ) => {
17
- const rendered = shallow ( < FEaaSComponent /> ) . find ( 'feaas-component' ) ;
18
- expect ( rendered ) . to . have . length ( 0 ) ;
21
+ describe ( 'composeComponentEndpoint' , ( ) => {
22
+ it ( 'should return endpoint with https when hostname from params is missing it' , ( ) => {
23
+ const endpoint = composeComponentEndpoint ( requiredParams ) ;
24
+ expect ( endpoint . startsWith ( 'https://' ) ) . to . equal ( true ) ;
25
+ } ) ;
19
26
} ) ;
20
27
21
- it ( 'should not render if missing required params' , ( ) => {
22
- const params : FEaaSComponentParams = {
23
- LibraryId : '' ,
24
- ComponentId : '' ,
25
- ComponentVersion : '' ,
26
- ComponentRevision : '' ,
27
- ComponentHostName : '' ,
28
- ComponentInstanceId : 'instance' ,
29
- ComponentHTMLOverride : 'html' ,
30
- ComponentDataOverride : '{}' ,
31
- } ;
32
- const rendered = shallow ( < FEaaSComponent params = { params } /> ) . find ( 'feaas-component' ) ;
33
- expect ( rendered ) . to . have . length ( 0 ) ;
28
+ it ( 'should not render with props and params missing' , ( ) => {
29
+ const wrapper = shallow ( < FEaaSComponent /> ) ;
30
+ expect ( wrapper ) . to . have . length ( 1 ) ;
31
+ expect ( wrapper . html ( ) ) . to . equal ( null ) ;
34
32
} ) ;
35
33
36
- it ( 'should render' , ( ) => {
37
- const wrapper = shallow ( < FEaaSComponent params = { requiredParams } /> ) ;
34
+ it ( 'should not render with props missing and only one param present' , ( ) => {
35
+ const props = {
36
+ params : {
37
+ ComponentHostName : 'host123' ,
38
+ } ,
39
+ } ;
40
+ const wrapper = shallow ( < FEaaSComponent { ...props } /> ) ;
38
41
expect ( wrapper ) . to . have . length ( 1 ) ;
39
- expect ( wrapper . html ( ) ) . to . contain (
40
- '<feaas-stylesheet library="library123" cdn="host123"></feaas-stylesheet>'
41
- ) ;
42
- expect ( wrapper . html ( ) ) . to . contain (
43
- '<feaas-component library="library123" cdn="host123" component="component123" version="version123" revision="revision123" data=""></feaas-component>'
44
- ) ;
45
- expect ( wrapper . html ( ) ) . to . contain (
46
- '<link rel="preload" as="style" href="host123/styles/library123/published.css"/>'
47
- ) ;
48
- expect ( wrapper . html ( ) ) . to . contain (
49
- '<link rel="preload" as="fetch" href="host123/components/library123/component123/version123/revision123.html"/>'
50
- ) ;
51
- expect ( wrapper . html ( ) ) . to . contain (
52
- '<link rel="preload" as="script" href="https://feaasstatic.blob.core.windows.net/packages/clientside/latest/browser/index.esm.js"/>'
53
- ) ;
42
+ expect ( wrapper . html ( ) ) . to . equal ( null ) ;
54
43
} ) ;
55
44
56
- it ( 'should render optional instance' , ( ) => {
57
- const params : FEaaSComponentParams = {
58
- ...requiredParams ,
59
- ComponentInstanceId : 'instance123' ,
60
- } ;
61
- const wrapper = shallow ( < FEaaSComponent params = { params } /> ) ;
45
+ it ( 'should render with template and last-modified when provided' , ( ) => {
46
+ const template = '<div>test output</div>' ;
47
+ const lastModified = 'March 1 2020' ;
48
+ const wrapper = shallow ( < FEaaSComponent template = { template } lastModified = { lastModified } /> ) ;
62
49
expect ( wrapper ) . to . have . length ( 1 ) ;
63
- expect ( wrapper . html ( ) ) . to . contain ( 'instance="instance123"' ) ;
50
+ expect ( wrapper . html ( ) ) . to . equal (
51
+ `<feaas-component last-modified="${ lastModified } ">${ template } </feaas-component>`
52
+ ) ;
64
53
} ) ;
65
54
66
- it ( 'should render optional html' , ( ) => {
67
- const params : FEaaSComponentParams = {
68
- ...requiredParams ,
69
- ComponentHTMLOverride : '<div>foo</div>' ,
55
+ it ( 'should render when only params are provided' , ( ) => {
56
+ const props = {
57
+ params : requiredParams ,
70
58
} ;
71
- const wrapper = shallow ( < FEaaSComponent params = { params } /> ) ;
59
+ const wrapper = shallow ( < FEaaSComponent { ... props } /> ) ;
72
60
expect ( wrapper ) . to . have . length ( 1 ) ;
73
- expect ( wrapper . html ( ) ) . to . contain ( params . ComponentHTMLOverride ) ;
61
+ expect ( wrapper . html ( ) ) . to . equal (
62
+ '<feaas-component cdn="host123" library="library123" version="version123" component="component123" revision="staged"></feaas-component>'
63
+ ) ;
74
64
} ) ;
75
65
76
66
describe ( 'data' , ( ) => {
77
67
it ( 'should send override data' , ( ) => {
78
- const params : FEaaSComponentParams = {
79
- ...requiredParams ,
80
- ComponentDataOverride : '{ "foo": "bar", "baz": 1 }' ,
68
+ const props : FEaaSComponentProps = {
69
+ params : {
70
+ ...requiredParams ,
71
+ ComponentDataOverride : '{ "foo": "bar", "baz": 1 }' ,
72
+ } ,
81
73
} ;
82
- const wrapper = shallow ( < FEaaSComponent params = { params } /> ) ;
74
+ const wrapper = shallow ( < FEaaSComponent { ... props } /> ) ;
83
75
expect ( wrapper ) . to . have . length ( 1 ) ;
84
76
expect ( wrapper . html ( ) ) . to . contain (
85
- `data="${ params . ComponentDataOverride ! . replace ( / " / g, '"' ) } "`
77
+ `data="${ props . params ? .ComponentDataOverride ! . replace ( / " / g, '"' ) } "`
86
78
) ;
87
79
} ) ;
88
80
@@ -108,6 +100,12 @@ describe('<FEaaSComponent />', () => {
108
100
} ,
109
101
} ,
110
102
} ;
103
+ const props : FEaaSComponentProps = {
104
+ params : {
105
+ ...requiredParams ,
106
+ } ,
107
+ fields,
108
+ } ;
111
109
const expectedData = {
112
110
_ : {
113
111
sampleText : 'Welcome to Sitecore JSS' ,
@@ -123,27 +121,31 @@ describe('<FEaaSComponent />', () => {
123
121
} ,
124
122
} ,
125
123
} ;
126
- const wrapper = shallow ( < FEaaSComponent params = { requiredParams } fields = { fields } /> ) ;
124
+ const wrapper = shallow ( < FEaaSComponent { ... props } /> ) ;
127
125
expect ( wrapper ) . to . have . length ( 1 ) ;
128
126
expect ( wrapper . html ( ) ) . to . contain (
129
127
`data="${ JSON . stringify ( expectedData ) . replace ( / " / g, '"' ) } "`
130
128
) ;
131
129
} ) ;
132
130
133
131
it ( 'should prefer override data over datasource fields' , ( ) => {
134
- const params : FEaaSComponentParams = {
135
- ...requiredParams ,
136
- ComponentDataOverride : '{ "foo": "bar", "baz": 1 }' ,
137
- } ;
138
132
const fields : ComponentFields = {
139
133
sampleText : {
140
134
value : 'Welcome to Sitecore JSS' ,
141
135
} ,
142
136
} ;
143
- const wrapper = shallow ( < FEaaSComponent params = { params } fields = { fields } /> ) ;
137
+ const props : FEaaSComponentProps = {
138
+ params : {
139
+ ...requiredParams ,
140
+ ComponentDataOverride : '{ "foo": "bar", "baz": 1 }' ,
141
+ } ,
142
+ fields,
143
+ } ;
144
+
145
+ const wrapper = shallow ( < FEaaSComponent { ...props } /> ) ;
144
146
expect ( wrapper ) . to . have . length ( 1 ) ;
145
147
expect ( wrapper . html ( ) ) . to . contain (
146
- `data="${ params . ComponentDataOverride ! . replace ( / " / g, '"' ) } "`
148
+ `data="${ props . params ? .ComponentDataOverride ! . replace ( / " / g, '"' ) } "`
147
149
) ;
148
150
} ) ;
149
151
} ) ;
0 commit comments