Skip to content

Commit 4422837

Browse files
Merge pull request #70 from LambdaTest/stage
Add custom viewport support in static stories
2 parents 2836089 + 481772c commit 4422837

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

commands/storybook.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ async function storybook(serve, options) {
116116
browsers: browsers,
117117
resolutions: resolutions,
118118
storyIds: storyIds,
119-
waitForTimeout: storybookConfig.waitForTimeout
119+
waitForTimeout: storybookConfig.waitForTimeout,
120+
customViewports: storybookConfig.customViewports
120121
},
121122
git: {
122123
branch: commit.branch,

commands/utils/config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ const defaultSmartUIConfig = {
66
'safari',
77
'edge'
88
],
9-
resolutions: [
9+
viewports: [
1010
[1920, 1080]
1111
],
1212
waitForTimeout: 0,
1313
include: [],
14-
exclude: []
14+
exclude: [],
15+
customViewports: []
1516
}
1617
};
1718

commands/utils/dom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
6666
form.append('commitId', commit.shortHash);
6767
form.append('commitAuthor', commit.author.name);
6868
form.append('commitMessage', commit.subject);
69-
69+
form.append('customViewports', JSON.stringify(storybookConfig.customViewports));
7070
githubURL = process.env.GITHUB_URL
7171
if (githubURL) {
7272
form.append('githubURL', githubURL);

commands/utils/validate.js

+54-8
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ function validateConfig(configFile) {
125125

126126
try {
127127
validateConfigBrowsers(storybookConfig.browsers);
128-
storybookConfig.resolutions = validateConfigResolutions(storybookConfig.resolutions);
128+
resolutions = storybookConfig.resolutions || storybookConfig.viewports
129+
storybookConfig.resolutions = validateConfigResolutions(resolutions);
130+
storybookConfig.viewports = storybookConfig.resolutions;
131+
validateCustomViewPorts(storybookConfig.customViewports)
129132
} catch (error) {
130133
console.log(`[smartui] Error: Invalid config, ${error.message}`);
131134
process.exit(constants.ERROR_CATCHALL);
@@ -136,7 +139,7 @@ function validateConfig(configFile) {
136139
storybookConfig.waitForTimeout = 0;
137140
} else if (storybookConfig.waitForTimeout <= 0 || storybookConfig.waitForTimeout > 30000) {
138141
console.log('[smartui] Warning: Invalid config, value of waitForTimeout must be > 0 and <= 30000');
139-
console.log('If you do not wish to include waitForTimeout parameter, remove it from the config file.');
142+
console.log('[smartui] If you do not wish to include waitForTimeout parameter, remove it from the config file.');
140143
storybookConfig.waitForTimeout = 0;
141144
}
142145

@@ -158,25 +161,31 @@ function validateConfigBrowsers(browsers) {
158161

159162
function validateConfigResolutions(resolutions) {
160163
if (!Array.isArray(resolutions)) {
161-
throw new ValidationError('invalid resolutions.');
164+
throw new ValidationError('Invalid viewports config. Please add atleast one viewport.');
162165
}
163166
if (resolutions.length == 0) {
164-
throw new ValidationError('empty resolutions list in config.');
167+
throw new ValidationError('Empty viewports list in config.');
165168
}
166169
if (resolutions.length > 5) {
167170
throw new ValidationError(`max resolutions: ${MAX_RESOLUTIONS}`);
168171
}
169172
let res = [];
170173
resolutions.forEach(element => {
171174
if (!Array.isArray(element) || element.length == 0 || element.length > 2) {
172-
throw new ValidationError('invalid resolutions.');
175+
throw new ValidationError('Invalid elements in viewports config.');
173176
}
174177
let width = element[0];
175178
let height = element[1];
176-
if (typeof width != 'number' || width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH) {
179+
if (typeof width != 'number') {
180+
width = Number(width);
181+
}
182+
if (typeof height != 'number') {
183+
height = Number(height);
184+
}
185+
if (width && width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH) {
177186
throw new ValidationError(`width must be > ${MIN_RESOLUTION_WIDTH}, < ${MAX_RESOLUTION_WIDTH}`);
178187
}
179-
if (height && (typeof height != 'number' || height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH)) {
188+
if (height & ( height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH)) {
180189
throw new ValidationError(`height must be > ${MIN_RESOLUTION_HEIGHT}, < ${MAX_RESOLUTION_HEIGHT}`);
181190
}
182191
res.push([width, height || 0]);
@@ -185,6 +194,42 @@ function validateConfigResolutions(resolutions) {
185194
return res
186195
}
187196

197+
198+
function validateCustomViewPorts(customViewports) {
199+
if (!Array.isArray(customViewports)) {
200+
return
201+
}
202+
if (customViewports && customViewports.length == 0) {
203+
return
204+
}
205+
customViewports.forEach(element => {
206+
if (!Array.isArray(element.stories) || element.stories == 0) {
207+
throw new ValidationError('Missing `stories` in customViewports config. please check the config file');
208+
}
209+
if(!element.styles || !element.styles?.width ){
210+
throw new ValidationError('Missing `styles` in customViewports key. Please check the config file');
211+
}
212+
213+
let width = element.styles.width;
214+
let height = element.styles.height;
215+
if (width && typeof width != 'number') {
216+
width = Number(width);
217+
}
218+
if (height && typeof height != 'number') {
219+
height = Number(height);
220+
}
221+
if (width && width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH) {
222+
throw new ValidationError(`customViewports.styles width must be > ${MIN_RESOLUTION_WIDTH}, < ${MAX_RESOLUTION_WIDTH}`);
223+
}
224+
if (height & ( height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH)) {
225+
throw new ValidationError(`customViewports.styles height must be > ${MIN_RESOLUTION_HEIGHT}, < ${MAX_RESOLUTION_HEIGHT}`);
226+
}
227+
element.styles.width = width;
228+
element.styles.height = height;
229+
});
230+
return
231+
}
232+
188233
module.exports = {
189234
ValidationError,
190235
validateProjectToken,
@@ -193,5 +238,6 @@ module.exports = {
193238
validateLatestBuild,
194239
validateConfig,
195240
validateConfigBrowsers,
196-
validateConfigResolutions
241+
validateConfigResolutions,
242+
validateCustomViewPorts
197243
};

0 commit comments

Comments
 (0)