Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Split test-amp-story-interactive-* test files to not recursively include the base test-amp-story-interactive tests in them #35289

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions extensions/amp-story-interactive/0.1/test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Returns mock interactive data.
*
* @return {Object}
*/
export const getMockInteractiveData = () => {
return {
options: [
{
index: 0,
count: 3,
selected: true,
},
{
index: 1,
count: 3,
selected: false,
},
{
index: 2,
count: 3,
selected: false,
},
{
index: 3,
count: 1,
selected: false,
},
],
};
};

/**
* Returns mock interactive data with index key values that don't match the
* index within the options array.
*
* @return {Object}
*/
export const getMockScrambledData = () => {
return {
options: [
{
index: 3,
count: 4,
selected: false,
},
{
index: 0,
count: 1,
selected: false,
},
{
index: 1,
count: 2,
selected: false,
},
{
index: 2,
count: 3,
selected: true,
},
],
};
};

/**
* Returns mock interactive data that doesn't account for all options.
*
* @return {Object}
*/
export const getMockIncompleteData = () => {
return {
options: [
{
index: 1,
count: 5,
selected: false,
},
{
index: 2,
count: 5,
selected: true,
},
],
};
};

/**
* Returns mock interactive data with index key values that don't correspond
* to any of the option elements.
*
* @return {Object}
*/
export const getMockOutOfBoundsData = () => {
return {
options: [
{
index: 3,
count: 4,
selected: false,
},
{
index: 0,
count: 1,
selected: true,
},
{
index: -1,
count: 2,
selected: false,
},
{
index: 4,
count: 3,
selected: false,
},
],
};
};

export const addConfigToInteractive = (
interactive,
options = 4,
correct = undefined,
attributes = ['text', 'results-category', 'image']
) => {
for (let i = 0; i < options; i++) {
attributes.forEach((attr) => {
interactive.element.setAttribute(
`option-${i + 1}-${attr}`,
`${attr} ${i + 1}`
);
});
}
if (correct) {
interactive.element.setAttribute(`option-${correct}-correct`, 'correct');
}
};

/**
* Populates the quiz with some number of prompts and some number of options.
*
* @param {AmpStoryInteractive} quiz
* @param {number} numOptions
* @param {?string} prompt
* @param {number} correctOption
*/
export const populateQuiz = (
quiz,
numOptions = 4,
prompt = undefined,
correctOption = 1
) => {
if (prompt) {
quiz.element.setAttribute('prompt-text', prompt);
}
addConfigToInteractive(quiz, numOptions, correctOption);
quiz.element.setAttribute('id', 'TEST_quizId');
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import {AmpStoryRequestService} from '../../../amp-story/1.0/amp-story-request-s
import {AmpStoryStoreService} from '../../../amp-story/1.0/amp-story-store-service';
import {LocalizationService} from '#service/localization';
import {Services} from '#service';
import {
addConfigToInteractive,
getMockInteractiveData,
} from './test-amp-story-interactive';
import {addConfigToInteractive, getMockInteractiveData} from './helpers';
import {measureMutateElementStub} from '#testing/test-helper';
import {registerServiceBuilder} from '../../../../src/service-helpers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getMockInteractiveData,
getMockOutOfBoundsData,
getMockScrambledData,
} from './test-amp-story-interactive';
} from './helpers';
import {measureMutateElementStub} from '#testing/test-helper';
import {registerServiceBuilder} from '../../../../src/service-helpers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
getMockInteractiveData,
getMockOutOfBoundsData,
getMockScrambledData,
} from './test-amp-story-interactive';
import {populateQuiz} from './test-amp-story-interactive-quiz';
populateQuiz,
} from './helpers';
import {registerServiceBuilder} from '../../../../src/service-helpers';

describes.realWin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getMockInteractiveData,
getMockOutOfBoundsData,
getMockScrambledData,
} from './test-amp-story-interactive';
} from './helpers';
import {measureMutateElementStub} from '#testing/test-helper';
import {registerServiceBuilder} from '../../../../src/service-helpers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,14 @@ import {AmpStoryStoreService} from '../../../amp-story/1.0/amp-story-store-servi
import {LocalizationService} from '#service/localization';
import {Services} from '#service';
import {
addConfigToInteractive,
getMockIncompleteData,
getMockInteractiveData,
getMockOutOfBoundsData,
getMockScrambledData,
} from './test-amp-story-interactive';
populateQuiz,
} from './helpers';
import {registerServiceBuilder} from '../../../../src/service-helpers';

/**
* Populates the quiz with some number of prompts and some number of options.
*
* @param {Window} win
* @param {AmpStoryInteractive} quiz
* @param {=number} numOptions
* @param {?string} prompt
* @param {=number} correctOption
*/
export const populateQuiz = (
quiz,
numOptions = 4,
prompt = undefined,
correctOption = 1
) => {
if (prompt) {
quiz.element.setAttribute('prompt-text', prompt);
}
addConfigToInteractive(quiz, numOptions, correctOption);
quiz.element.setAttribute('id', 'TEST_quizId');
};

describes.realWin(
'amp-story-interactive-quiz',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import {InteractiveType} from '../amp-story-interactive-abstract';
import {LocalizationService} from '#service/localization';
import {Services} from '#service';
import {addConfigToInteractive} from './test-amp-story-interactive';
import {addConfigToInteractive} from './helpers';
import {registerServiceBuilder} from '../../../../src/service-helpers';

const addThresholdsToInteractive = (interactive, thresholdList) => {
Expand Down
Loading