Skip to content

Commit

Permalink
Added new ConvoTest class that offers extra features for testing Conv…
Browse files Browse the repository at this point in the history
…o agents.
  • Loading branch information
tmtek committed Nov 2, 2018
1 parent 68d679e commit 89e4fdb
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 75 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ const { Convo } = require('./src/convo');
const { ConvoApp } = require('./src/convo-app');
const { Say } = require('./src/say');
const { ConvoStorage } = require('./src/convo-storage');
const { ConvoTest } = require('./src/convo-test');

module.exports = { Convo, ConvoApp, Say, ConvoStorage };
module.exports = { Convo, ConvoApp, Say, ConvoStorage, ConvoTest };
34 changes: 34 additions & 0 deletions src/convo-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

class ConvoTest {
static containsResponseType(requests, types) {
if (!requests) {
throw new Error('Requests were not supplied.');
}
if (!types || types.length === 0) {
return false;
}
return types
.map(type => requests.filter(request => request.payload.type === type).length > 0)
.filter(typeVal => !typeVal)
.length === 0;
}
static isConversationClose(requests) {
return requests[0] && requests[0].action === 'close';
}
static testConversation(conversation, done) {
if (!conversation || !conversation.then) {
throw new Error('You must pass a conversational promise.');
}
if (!done) {
throw new Error('You must pass a done function.');
}
let isDone = false;
conversation.catch(err => {
isDone = true;
done(err);
})
.then(() => !isDone && done());
}
}

module.exports = { ConvoTest };
100 changes: 26 additions & 74 deletions test/convo-app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let { Convo } = require('../src/convo');
let { ConvoApp } = require('../src/convo-app');
let { ConvoTest } = require('../src/convo-test');
let assert = require('assert');

describe('ConvoApp', () => {
Expand Down Expand Up @@ -261,8 +262,7 @@ describe('ConvoApp', () => {
}

}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => app.intent(new Convo(convo), 'list_repeat'))
.then(({ app, convo, requests }) => {
Expand All @@ -275,11 +275,7 @@ describe('ConvoApp', () => {
assert(listLength === 5);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able to page through list with list_next and have it loop back.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -301,7 +297,7 @@ describe('ConvoApp', () => {
}

}
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => {
assert(requests && requests.length > 0);
Expand Down Expand Up @@ -346,7 +342,7 @@ describe('ConvoApp', () => {
assert(listLength === 5);
return { app, convo, requests };
})
.then(() => done());
, done);
});
it('Should be able to page through list with list_prev and have it loop back.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -368,8 +364,7 @@ describe('ConvoApp', () => {
}

}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => {
assert(requests && requests.length > 0);
Expand Down Expand Up @@ -414,11 +409,7 @@ describe('ConvoApp', () => {
assert(listLength === 5);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able to page through list with list_next and have it loop back while changing page sizes.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -440,7 +431,7 @@ describe('ConvoApp', () => {
}

}
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => {
assert(requests && requests.length > 0);
Expand Down Expand Up @@ -485,7 +476,7 @@ describe('ConvoApp', () => {
assert(listLength === 5);
return { app, convo, requests };
})
.then(() => done());
, done);
});
it('Should be able to page through list with list_prev and have it loop back while changing page sizes.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -507,8 +498,7 @@ describe('ConvoApp', () => {
}

}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => {
assert(requests && requests.length > 0);
Expand Down Expand Up @@ -553,11 +543,7 @@ describe('ConvoApp', () => {
assert(listLength === 5);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able to use list_all to move from a paged list to full list.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -579,8 +565,7 @@ describe('ConvoApp', () => {
}

}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => {
assert(requests && requests.length > 0);
Expand All @@ -603,11 +588,7 @@ describe('ConvoApp', () => {
assert(listLength === 5);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able to use list_select to select an item in the full list.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -634,8 +615,7 @@ describe('ConvoApp', () => {
}));
}
}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => app.intent(new Convo(convo), 'list_select', { index: 2 }))
.then(({ app, convo, requests }) => {
Expand All @@ -645,11 +625,7 @@ describe('ConvoApp', () => {
assert(item === 'test 2', `Was actually ${item}`);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able to use list_select_ui to select an item in the full list.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -676,8 +652,7 @@ describe('ConvoApp', () => {
}));
}
}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => app.intent(new Convo(convo), 'list_select_ui', null, 'item_1'))
.then(({ app, convo, requests }) => {
Expand All @@ -687,11 +662,7 @@ describe('ConvoApp', () => {
assert(item === 'test 2', `Was actually ${item}`);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able to use list_select to select an item in on a list page.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -718,8 +689,7 @@ describe('ConvoApp', () => {
}));
}
}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => app.intent(new Convo(convo), 'list_select', { index: 2 }))
.then(({ app, convo, requests }) => {
Expand All @@ -729,11 +699,7 @@ describe('ConvoApp', () => {
assert(item === 'test 4', `Was actually ${item}`);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able select the next item after a currently select one with list_select_next and have it loop.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -760,8 +726,7 @@ describe('ConvoApp', () => {
}));
}
}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => app.intent(new Convo(convo), 'list_select', { index: 2 }))
.then(({ app, convo, requests }) => {
Expand Down Expand Up @@ -803,11 +768,7 @@ describe('ConvoApp', () => {
assert(item === 'test 1', `Was actually ${item}`);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
it('Should be able select the previous item before a currently select one with list_select_prev and have it loop.', (done) => {
let list = ['test 1', 'test 2', 'test 3', 'test 4', 'test 5'];
Expand All @@ -834,8 +795,8 @@ describe('ConvoApp', () => {
}));
}
}
let didError = false;
new MyApp()

ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'applyList')
.then(({ app, convo, requests }) => app.intent(new Convo(convo), 'list_select', { index: 2 }))
.then(({ app, convo, requests }) => {
Expand Down Expand Up @@ -877,11 +838,7 @@ describe('ConvoApp', () => {
assert(item === 'test 3', `Was actually ${item}`);
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
});

Expand Down Expand Up @@ -921,19 +878,14 @@ describe('ConvoApp', () => {
];
}
}
let didError = false;
new MyApp()
ConvoTest.testConversation(new MyApp()
.intent(new Convo(), 'help')
.then(({ app, convo, requests }) => {
assert(requests && requests.length > 0);
assert(convo.hasList('help'));
return { app, convo, requests };
})
.catch(err => {
didError = true;
return done(err);
})
.then(() => !didError && done());
, done);
});
});

Expand Down
Loading

0 comments on commit 89e4fdb

Please sign in to comment.