-
Notifications
You must be signed in to change notification settings - Fork 25
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
FR: Add fake Auth plugin object for use in unit tests #30
Comments
@mfickett Thanks for submitting the feature request. Internal Ref: OKTA-343960 |
In case it's helpful, here's my /* eslint-disable */
// Many unused parameters and uses of any.
/**
* A fake Okta-Vue plugin for use in unit tests.
*
* Vue plugin guide: https://vuejs.org/v2/guide/plugins.html
* modeled after the real Okta-Vue plugin, https://github.com/okta/okta-vue/blob/master/src/Auth.js
* typings reference:
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/okta__okta-vue/index.d.ts
*
* In tests, install this with
* import { fakeOkta } from "@/testing/fakeokta";
* Vue.use(fakeOkta);
* It is installed on Vue (not just localVue) since components use the global Vue.prototype.$auth.
* Related docs:
* https://vue-test-utils.vuejs.org/guides/common-tips.html#applying-global-plugins-and-mixins
*/
import { VueConstructor } from "vue";
class FakeAuth {
authenticated: boolean;
constructor() {
this.authenticated = true;
}
setAuthenticated(authenticated: boolean) {
this.authenticated = authenticated;
}
async loginRedirect(fromUri?: string, additionalParams?: any): Promise<void> {
return;
}
async logout(): Promise<void> {
return;
}
async isAuthenticated(): Promise<boolean> {
return this.authenticated;
}
async getIdToken(): Promise<string> {
return "header.someidtoken.signature";
}
async getAccessToken(): Promise<string> {
return "header.someaccesstoken.signature";
}
async getUser(): Promise<any> {
return { name: "Fake User", preferred_username: "[email protected]" };
}
async handleAuthentication(): Promise<void> {
return;
}
getFromUri(): string {
return "/";
}
}
function install(Vue: VueConstructor, options: any) {
Vue.prototype.$auth = new FakeAuth();
}
export default { install }; I ended up having some issues because I call import { assert } from "chai";
import { createLocalVue, shallowMount } from "@vue/test-utils";
import Vue from "vue";
import App from "@/App.vue";
import fakeOkta from "@/testing/fakeokta";
const localVue = createLocalVue();
// Even though we're using shallowMount which does not render subcomponents, install VueRouter
Vue.use(fakeOkta);
describe("App.vue", () => {
it("renders a message that the user is not logged in when $auth.isAuthenticated is false", async () => {
Vue.prototype.$auth.setAuthenticated(false);
const wrapper = shallowMount(App, { localVue });
// Although the created() lifecycle hook is called by shallowMount, pollAuthenticated() is async
// (and must be b/c Okta's API is async) and created() is synchronous, so we must await the call
// here to force its processing before the asserts below.
await wrapper.vm.pollAuthenticated();
assert.match(wrapper.text(), /Not Authenticated/);
});
}); I'm not confident that this usage is better than mocking, but I'm anticipating more complex cases where faking may work better. |
I'm submitting a:
Current behavior
Tests either fail because they don't have
Auth
installed, or try to use the realAuth
.Expected behavior
It would be great to have a fake Vue plugin for Okta auth. Example usage might look like:
This would allow testing components that use
$auth
, with configurable behavior from theokta-vue
plugin.The implementation could presumably be similar to the real plugin with an install function, but then have simple setters/getters instead of the full
Auth
implementation.The text was updated successfully, but these errors were encountered: