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

add more tests for State classes and CardFactory #498

Merged
merged 1 commit into from
Sep 27, 2018
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
61 changes: 44 additions & 17 deletions libraries/botbuilder-core/tests/cardFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ function assertActions(actions, count, titles) {
assert(Array.isArray(actions), `actions not array.`);
assert(actions.length === count, `wrong number of actions returned.`);
for (let i = 0; i < count; i++) {
assert(actions[i].title, `title[${i}] missing`);
assert(actions[i].title, `title[${ i }] missing`);
if (titles) {
assert(actions[i].title === titles[i], `title[${i}] invalid`);
assert(actions[i].title === titles[i], `title[${ i }] invalid`);
}
assert(actions[i].type, `type[${i}] missing`);
assert(actions[i].value, `value[${i}] missing`);
assert(actions[i].type, `type[${ i }] missing`);
assert(actions[i].value, `value[${ i }] missing`);
}
}

function assertImages(images, count, links) {
assert(Array.isArray(images), `images not array.`);
assert(images.length === count, `wrong number of images returned.`);
for (let i = 0; i < count; i++) {
assert(images[i].url, `image url[${i}] missing`);
assert(images[i].url, `image url[${ i }] missing`);
if (links) {
assert(images[i].url === links[i], `image url[${i}] invalid`);
assert(images[i].url === links[i], `image url[${ i }] invalid`);
}
}
}
Expand All @@ -35,13 +35,22 @@ function assertMedia(media, count, links) {
assert(Array.isArray(media), `media not array.`);
assert(media.length === count, `wrong number of media returned.`);
for (let i = 0; i < count; i++) {
assert(media[i].url, `media url[${i}] missing`);
assert(media[i].url, `media url[${ i }] missing`);
if (links) {
assert(media[i].url === links[i], `media url[${i}] invalid`);
assert(media[i].url === links[i], `media url[${ i }] invalid`);
}
}
}

function assertOAuthActions(actions, title) {
assert(Array.isArray(actions), `received actions is not an array.`);
assert(actions.length === 1, `should have received only one action.`);
const button = actions[0];
assert(button.value === undefined, `OAuthCard actions' value should be undefined.`);
assert(button.title === title, `action's title [${ button.title }] is not expected "${ title }".`);
assert(button.type === 'signin', `action's type [${button.type}] is not expected "signin".`);
}

describe(`CardFactory`, function () {
this.timeout(5000);

Expand All @@ -51,7 +60,7 @@ describe(`CardFactory`, function () {
});

it(`should support an array of CardAction options passed to actions().`, function () {
const actions = CardFactory.actions([{
const actions = CardFactory.actions([{
title: 'foo',
type: 'postBack',
value: 'bar'
Expand All @@ -72,7 +81,7 @@ describe(`CardFactory`, function () {
});

it(`should support an array of CardImage options passed to images().`, function () {
const images = CardFactory.images([{
const images = CardFactory.images([{
url: 'foo',
alt: 'bar',
tap: {}
Expand All @@ -93,7 +102,7 @@ describe(`CardFactory`, function () {
});

it(`should support an array of CardMedia options passed to media().`, function () {
const media = CardFactory.media([{
const media = CardFactory.media([{
url: 'foo',
profile: 'bar'
}]);
Expand All @@ -105,13 +114,13 @@ describe(`CardFactory`, function () {
const media = CardFactory.media(undefined);
assertMedia(media, 0);
});

it(`should create an adaptiveCard().`, function () {
const attachment = CardFactory.adaptiveCard({ type: 'AdaptiveCard' });
assertAttachment(attachment, CardFactory.contentTypes.adaptiveCard);
assert(attachment.content.type, `wrong content.`);
});

it(`should create an animationCard().`, function () {
const links = ['https://example.org/media'];
const attachment = CardFactory.animationCard('foo', links);
Expand Down Expand Up @@ -221,7 +230,7 @@ describe(`CardFactory`, function () {
const content = attachment.content;
assert(content.title === 'foo', `wrong title.`);
});

it(`should create a thumbnailCard() with images.`, function () {
const links = ['https://example.org/image'];
const attachment = CardFactory.thumbnailCard('foo', links);
Expand All @@ -230,7 +239,7 @@ describe(`CardFactory`, function () {
assert(content.title === 'foo', `wrong title.`);
assertImages(content.images, 1, links);
});

it(`should create a thumbnailCard() with text.`, function () {
const attachment = CardFactory.thumbnailCard('foo', 'bar');
assertAttachment(attachment, CardFactory.contentTypes.thumbnailCard);
Expand All @@ -248,7 +257,7 @@ describe(`CardFactory`, function () {
assert(content.text === 'bar', `wrong text.`);
assertImages(content.images, 1, links);
});

it(`should create a thumbnailCard() with buttons.`, function () {
const attachment = CardFactory.thumbnailCard('foo', undefined, ['a', 'b', 'c']);
assertAttachment(attachment, CardFactory.contentTypes.thumbnailCard);
Expand All @@ -264,7 +273,7 @@ describe(`CardFactory`, function () {
assert(content.title === undefined, `wrong title.`);
assertActions(content.buttons, 3, ['a', 'b', 'c']);
});

it(`should create a thumbnailCard() with text and buttons.`, function () {
const attachment = CardFactory.thumbnailCard('foo', 'bar', undefined, ['a', 'b', 'c']);
assertAttachment(attachment, CardFactory.contentTypes.thumbnailCard);
Expand Down Expand Up @@ -316,4 +325,22 @@ describe(`CardFactory`, function () {
assert(content.buttons[0].value === 'https://example.org/signin', `wrong action value.`);
assert(content.text === 'bar', `wrong text.`);
});

it(`should create an OAuthCard with text.`, function () {
const attachment = CardFactory.oauthCard('ConnectionName', 'Test', 'Test-text');
assertAttachment(attachment, CardFactory.contentTypes.oauthCard);
const content = attachment.content;
assertOAuthActions(content.buttons, 'Test');
assert(content.text === 'Test-text', `wrong text.`);
assert(content.connectionName === 'ConnectionName', `wrong connectionName.`);
});

it(`should create an OAuthCard without text.`, function () {
const attachment = CardFactory.oauthCard('ConnectionName', 'Test');
assertAttachment(attachment, CardFactory.contentTypes.oauthCard);
const content = attachment.content;
assertOAuthActions(content.buttons, 'Test');
assert(content.text === undefined, `text should not exist.`);
assert(content.connectionName === 'ConnectionName', `wrong connectionName.`);
});
});
12 changes: 12 additions & 0 deletions libraries/botbuilder-core/tests/conversationState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ describe(`ConversationState`, function () {
done();
}
});

it(`should throw NO_KEY error if getStorageKey() returns falsey value.`, async function () {
conversationState.getStorageKey = turnContext => undefined;
try {
await conversationState.load(context, true);
} catch (err) {
assert(err.message === 'ConversationState: channelId and/or conversation missing from context.request.',
`unexpected Error.message received: ${err.message}`);
return;
}
assert(false, `should have thrown an error.`);
});
});
12 changes: 12 additions & 0 deletions libraries/botbuilder-core/tests/privateConversationState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ describe(`PrivateConversationState`, function () {
done();
}
});

it(`should throw NO_KEY error if getStorageKey() returns falsey value.`, async function () {
privateConversationState.getStorageKey = turnContext => undefined;
try {
await privateConversationState.load(context, true);
} catch (err) {
assert(err.message === 'PrivateConversationState: channelId and/or PrivateConversation missing from context.request.',
`unexpected Error.message received: ${err.message}`);
return;
}
assert(false, `should have thrown an error.`);
});
});
12 changes: 12 additions & 0 deletions libraries/botbuilder-core/tests/userState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@ describe(`UserState`, function () {
done();
}
});

it(`should throw NO_KEY error if getStorageKey() returns falsey value.`, async function () {
userState.getStorageKey = turnContext => undefined;
try {
await userState.load(context, true);
} catch (err) {
assert(err.message === 'UserState: channelId and/or conversation missing from context.request.',
`unexpected Error.message received: ${err.message}`);
return;
}
assert(false, `should have thrown an error.`);
});
});