下面是代码。
signUp(userData)
{
return new Promise((resolved, reject) => {
const userPoolData = new AWSCognito.CognitoUserPool(this._POOL_DATA);
let userAttribute = [];
userAttribute.push(
new AWSCognito.CognitoUserAttribute({ Name: "email", Value: userData.email }),
new AWSCognito.CognitoUserAttribute({ Name: "custom:role", Value: userData.role }),
);
userPoolData.signUp(userData.email, userData.password, userAttribute, null, function (err, result)
{
if (err) {
reject(err);
}
else {
resolved(result);
}
});
});
}
import * as AWS from 'the-dependency'; // dependency where AWSCognito comes from
it('should sign up user', async done => {
// CognitoUserPool return this
spyOn(AWS.AWSCognito, 'CognitoUserPool').and.returnValue({
signUp: (email, password, userAttribute, fn) => fn(null, true); // may have to check the mock
});;
const result = await component.signUp({ email: '...', password: '...', role: '...' });
// wait until pending promises are done
await fixture.whenStable();
expect(result).toBe(true); // resolved it to true in `signUp` mock
});
签出:Jasmine - How to write test case for AWS service authenticateUser?