diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-actions-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-actions-test.js index d5aa28fc4c0..191831f4180 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/composer-actions-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/composer-actions-test.js @@ -18,6 +18,244 @@ acceptance("Composer Actions", { site: { can_tag_topics: true, }, + pretend(server) { + server.get("/t/130.json", () => { + return [ + 200, + { "Content-Type": "application/json" }, + { + post_stream: { + posts: [ + { + id: 133, + name: null, + username: "bianca", + avatar_template: + "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", + created_at: "2020-07-05T09:28:36.371Z", + cooked: + "
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas a varius ipsum. Nunc euismod, metus non vulputate malesuada, ligula metus pharetra tortor, vel sodales arcu lacus sed mauris. Nam semper, orci vitae fringilla placerat, dui tellus convallis felis, ultricies laoreet sapien mi et metus. Mauris facilisis, mi fermentum rhoncus feugiat, dolor est vehicula leo, id porta leo ex non enim. In a ligula vel tellus commodo scelerisque non in ex. Pellentesque semper leo quam, nec varius est viverra eget. Donec vehicula sem et massa faucibus tempus.
", + post_number: 1, + post_type: 1, + updated_at: "2020-07-05T09:28:36.371Z", + reply_count: 0, + reply_to_post_number: null, + quote_count: 0, + incoming_link_count: 0, + reads: 1, + readers_count: 0, + score: 0, + yours: true, + topic_id: 130, + topic_slug: "lorem-ipsum-dolor-sit-amet", + display_username: null, + primary_group_name: null, + primary_group_flair_url: null, + primary_group_flair_bg_color: null, + primary_group_flair_color: null, + version: 1, + can_edit: true, + can_delete: false, + can_recover: false, + can_wiki: true, + read: true, + user_title: "Tester", + title_is_group: false, + actions_summary: [ + { + id: 3, + can_act: true, + }, + { + id: 4, + can_act: true, + }, + { + id: 8, + can_act: true, + }, + { + id: 7, + can_act: true, + }, + ], + moderator: false, + admin: true, + staff: true, + user_id: 1, + hidden: false, + trust_level: 0, + deleted_at: null, + user_deleted: false, + edit_reason: null, + can_view_edit_history: true, + wiki: false, + reviewable_id: 0, + reviewable_score_count: 0, + reviewable_score_pending_count: 0, + }, + ], + stream: [133], + }, + timeline_lookup: [[1, 0]], + related_messages: [], + suggested_topics: [], + id: 130, + title: "Lorem ipsum dolor sit amet", + fancy_title: "Lorem ipsum dolor sit amet", + posts_count: 1, + created_at: "2020-07-05T09:28:36.260Z", + views: 1, + reply_count: 0, + like_count: 0, + last_posted_at: "2020-07-05T09:28:36.371Z", + visible: true, + closed: false, + archived: false, + has_summary: false, + archetype: "private_message", + slug: "lorem-ipsum-dolor-sit-amet", + category_id: null, + word_count: 86, + deleted_at: null, + user_id: 1, + featured_link: null, + pinned_globally: false, + pinned_at: null, + pinned_until: null, + image_url: null, + draft: null, + draft_key: "topic_130", + draft_sequence: 0, + posted: true, + unpinned: null, + pinned: false, + current_post_number: 1, + highest_post_number: 1, + last_read_post_number: 1, + last_read_post_id: 133, + deleted_by: null, + has_deleted: false, + actions_summary: [ + { + id: 4, + count: 0, + hidden: false, + can_act: true, + }, + { + id: 8, + count: 0, + hidden: false, + can_act: true, + }, + { + id: 7, + count: 0, + hidden: false, + can_act: true, + }, + ], + chunk_size: 20, + bookmarked: false, + message_archived: false, + topic_timer: null, + message_bus_last_id: 5, + participant_count: 1, + pm_with_non_human_user: false, + show_read_indicator: false, + requested_group_name: null, + thumbnails: null, + tags_disable_ads: false, + details: { + notification_level: 3, + notifications_reason_id: 1, + can_move_posts: true, + can_edit: true, + can_delete: true, + can_remove_allowed_users: true, + can_invite_to: true, + can_invite_via_email: true, + can_create_post: true, + can_reply_as_new_topic: true, + can_flag_topic: true, + can_convert_topic: true, + can_review_topic: true, + can_remove_self_id: 1, + participants: [ + { + id: 1, + username: "bianca", + name: null, + avatar_template: + "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", + post_count: 1, + primary_group_name: null, + primary_group_flair_url: null, + primary_group_flair_color: null, + primary_group_flair_bg_color: null, + }, + ], + allowed_users: [ + { + id: 7, + username: "foo", + name: null, + avatar_template: + "/letter_avatar_proxy/v4/letter/f/b19c9b/{size}.png", + }, + ], + created_by: { + id: 1, + username: "bianca", + name: null, + avatar_template: + "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", + }, + last_poster: { + id: 1, + username: "bianca", + name: null, + avatar_template: + "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", + }, + allowed_groups: [ + { + id: 43, + automatic: false, + name: "foo_group", + user_count: 4, + mentionable_level: 0, + messageable_level: 99, + visibility_level: 0, + automatic_membership_email_domains: "", + primary_group: false, + title: null, + grant_trust_level: null, + incoming_email: null, + has_messages: true, + flair_url: null, + flair_bg_color: "", + flair_color: "", + bio_raw: null, + bio_cooked: null, + bio_excerpt: null, + public_admission: false, + public_exit: false, + allow_membership_requests: false, + full_name: null, + default_notification_level: 3, + membership_request_template: null, + members_visibility_level: 0, + can_see_members: true, + publish_read_state: false, + }, + ], + }, + }, + ]; + }); + }, }); test("creating new topic and then reply_as_private_message keeps attributes", async (assert) => { @@ -154,244 +392,6 @@ test("reply_as_new_topic without a new_topic draft", async (assert) => { }); test("reply_as_new_group_message", async (assert) => { - // eslint-disable-next-line - server.get("/t/130.json", () => { - return [ - 200, - { "Content-Type": "application/json" }, - { - post_stream: { - posts: [ - { - id: 133, - name: null, - username: "bianca", - avatar_template: - "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", - created_at: "2020-07-05T09:28:36.371Z", - cooked: - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas a varius ipsum. Nunc euismod, metus non vulputate malesuada, ligula metus pharetra tortor, vel sodales arcu lacus sed mauris. Nam semper, orci vitae fringilla placerat, dui tellus convallis felis, ultricies laoreet sapien mi et metus. Mauris facilisis, mi fermentum rhoncus feugiat, dolor est vehicula leo, id porta leo ex non enim. In a ligula vel tellus commodo scelerisque non in ex. Pellentesque semper leo quam, nec varius est viverra eget. Donec vehicula sem et massa faucibus tempus.
", - post_number: 1, - post_type: 1, - updated_at: "2020-07-05T09:28:36.371Z", - reply_count: 0, - reply_to_post_number: null, - quote_count: 0, - incoming_link_count: 0, - reads: 1, - readers_count: 0, - score: 0, - yours: true, - topic_id: 130, - topic_slug: "lorem-ipsum-dolor-sit-amet", - display_username: null, - primary_group_name: null, - primary_group_flair_url: null, - primary_group_flair_bg_color: null, - primary_group_flair_color: null, - version: 1, - can_edit: true, - can_delete: false, - can_recover: false, - can_wiki: true, - read: true, - user_title: "Tester", - title_is_group: false, - actions_summary: [ - { - id: 3, - can_act: true, - }, - { - id: 4, - can_act: true, - }, - { - id: 8, - can_act: true, - }, - { - id: 7, - can_act: true, - }, - ], - moderator: false, - admin: true, - staff: true, - user_id: 1, - hidden: false, - trust_level: 0, - deleted_at: null, - user_deleted: false, - edit_reason: null, - can_view_edit_history: true, - wiki: false, - reviewable_id: 0, - reviewable_score_count: 0, - reviewable_score_pending_count: 0, - }, - ], - stream: [133], - }, - timeline_lookup: [[1, 0]], - related_messages: [], - suggested_topics: [], - id: 130, - title: "Lorem ipsum dolor sit amet", - fancy_title: "Lorem ipsum dolor sit amet", - posts_count: 1, - created_at: "2020-07-05T09:28:36.260Z", - views: 1, - reply_count: 0, - like_count: 0, - last_posted_at: "2020-07-05T09:28:36.371Z", - visible: true, - closed: false, - archived: false, - has_summary: false, - archetype: "private_message", - slug: "lorem-ipsum-dolor-sit-amet", - category_id: null, - word_count: 86, - deleted_at: null, - user_id: 1, - featured_link: null, - pinned_globally: false, - pinned_at: null, - pinned_until: null, - image_url: null, - draft: null, - draft_key: "topic_130", - draft_sequence: 0, - posted: true, - unpinned: null, - pinned: false, - current_post_number: 1, - highest_post_number: 1, - last_read_post_number: 1, - last_read_post_id: 133, - deleted_by: null, - has_deleted: false, - actions_summary: [ - { - id: 4, - count: 0, - hidden: false, - can_act: true, - }, - { - id: 8, - count: 0, - hidden: false, - can_act: true, - }, - { - id: 7, - count: 0, - hidden: false, - can_act: true, - }, - ], - chunk_size: 20, - bookmarked: false, - message_archived: false, - topic_timer: null, - message_bus_last_id: 5, - participant_count: 1, - pm_with_non_human_user: false, - show_read_indicator: false, - requested_group_name: null, - thumbnails: null, - tags_disable_ads: false, - details: { - notification_level: 3, - notifications_reason_id: 1, - can_move_posts: true, - can_edit: true, - can_delete: true, - can_remove_allowed_users: true, - can_invite_to: true, - can_invite_via_email: true, - can_create_post: true, - can_reply_as_new_topic: true, - can_flag_topic: true, - can_convert_topic: true, - can_review_topic: true, - can_remove_self_id: 1, - participants: [ - { - id: 1, - username: "bianca", - name: null, - avatar_template: - "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", - post_count: 1, - primary_group_name: null, - primary_group_flair_url: null, - primary_group_flair_color: null, - primary_group_flair_bg_color: null, - }, - ], - allowed_users: [ - { - id: 7, - username: "foo", - name: null, - avatar_template: - "/letter_avatar_proxy/v4/letter/f/b19c9b/{size}.png", - }, - ], - created_by: { - id: 1, - username: "bianca", - name: null, - avatar_template: - "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", - }, - last_poster: { - id: 1, - username: "bianca", - name: null, - avatar_template: - "/letter_avatar_proxy/v4/letter/b/3be4f8/{size}.png", - }, - allowed_groups: [ - { - id: 43, - automatic: false, - name: "foo_group", - user_count: 4, - mentionable_level: 0, - messageable_level: 99, - visibility_level: 0, - automatic_membership_email_domains: "", - primary_group: false, - title: null, - grant_trust_level: null, - incoming_email: null, - has_messages: true, - flair_url: null, - flair_bg_color: "", - flair_color: "", - bio_raw: null, - bio_cooked: null, - bio_excerpt: null, - public_admission: false, - public_exit: false, - allow_membership_requests: false, - full_name: null, - default_notification_level: 3, - membership_request_template: null, - members_visibility_level: 0, - can_see_members: true, - publish_read_state: false, - }, - ], - }, - }, - ]; - }); - await visit("/t/lorem-ipsum-dolor-sit-amet/130"); await click(".create.reply"); const composerActions = selectKit(".composer-actions"); diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js index db064826e48..92c9565db99 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js @@ -10,8 +10,8 @@ import { Promise } from "rsvp"; acceptance("Composer", { loggedIn: true, - pretend(pretenderServer, helper) { - pretenderServer.post("/uploads/lookup-urls", () => { + pretend(server, helper) { + server.post("/uploads/lookup-urls", () => { return helper.response([]); }); }, @@ -810,20 +810,3 @@ test("Image resizing buttons", async (assert) => { "it does not unescapes script tags in code blocks" ); }); - -test("can reply to a private message", async (assert) => { - let submitted; - - /* global server */ - server.post("/posts", () => { - submitted = true; - return [200, { "Content-Type": "application/json" }, {}]; - }); - - await visit("/t/34"); - await click(".topic-post:eq(0) button.reply"); - await fillIn(".d-editor-input", "this is the *content* of the reply"); - await click("#reply-control button.create"); - - assert.ok(submitted); -}); diff --git a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js index 2677a11cf4c..5bc446fd597 100644 --- a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js +++ b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js @@ -41,6 +41,10 @@ export let fixturesByUrl; export default new Pretender(); +export function pretenderHelpers() { + return { parsePostData, response, success }; +} + export function applyDefaultHandlers(pretender) { // Autoload any `*-pretender` files Object.keys(requirejs.entries).forEach((e) => { diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index f386ba17459..f69f7361fed 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -1,8 +1,6 @@ import { Promise } from "rsvp"; import { isEmpty } from "@ember/utils"; import { later } from "@ember/runloop"; -/* global QUnit, resetSite */ - import sessionFixtures from "discourse/tests/fixtures/session-fixtures"; import HeaderComponent from "discourse/components/site-header"; import { forceMobile, resetMobile } from "discourse/lib/mobile"; @@ -36,7 +34,10 @@ import { setURLContainer } from "discourse/lib/url"; import { setDefaultOwner } from "discourse-common/lib/get-owner"; import bootbox from "bootbox"; import { moduleFor } from "ember-qunit"; -import { module } from "qunit"; +import QUnit, { module } from "qunit"; +import siteFixtures from "discourse/tests/fixtures/site-fixtures"; +import Site from "discourse/models/site"; +import createStore from "discourse/tests/helpers/create-store"; export function currentUser() { return User.create(sessionFixtures["/session/current.json"].current_user); @@ -114,6 +115,13 @@ $.fn.modal = AcceptanceModal; let _pretenderCallbacks = {}; +export function resetSite(siteSettings, extras) { + let siteAttrs = $.extend({}, siteFixtures["site.json"].site, extras || {}); + siteAttrs.store = createStore(); + siteAttrs.siteSettings = siteSettings; + return Site.resetCurrent(Site.create(siteAttrs)); +} + export function applyPretender(name, server, helper) { const cb = _pretenderCallbacks[name]; if (cb) { @@ -152,14 +160,19 @@ export function discourseModule(name, hooks) { }); } +export function addPretenderCallback(name, fn) { + if (name && fn) { + _pretenderCallbacks[name] = fn; + } +} + export function acceptance(name, options) { + name = `Acceptance: ${name}`; options = options || {}; - if (options.pretend) { - _pretenderCallbacks[name] = options.pretend; - } + addPretenderCallback(name, options.pretend); - module("Acceptance: " + name, { + module(name, { beforeEach() { resetMobile(); diff --git a/app/assets/javascripts/discourse/tests/helpers/widget-test.js b/app/assets/javascripts/discourse/tests/helpers/widget-test.js index 0a7e7d2780d..91f8daaa48d 100644 --- a/app/assets/javascripts/discourse/tests/helpers/widget-test.js +++ b/app/assets/javascripts/discourse/tests/helpers/widget-test.js @@ -1,10 +1,14 @@ import { moduleForComponent } from "ember-qunit"; import componentTest from "discourse/tests/helpers/component-test"; +import { addPretenderCallback } from "discourse/tests/helpers/qunit-helpers"; export function moduleForWidget(name, options = {}) { + let fullName = `widget:${name}`; + addPretenderCallback(fullName, options.pretend); + moduleForComponent( name, - `widget:${name}`, + fullName, Object.assign( { integration: true }, { beforeEach: options.beforeEach, afterEach: options.afterEach } diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js new file mode 100644 index 00000000000..dc2ed5749da --- /dev/null +++ b/app/assets/javascripts/discourse/tests/setup-tests.js @@ -0,0 +1,189 @@ +import { + resetSettings, + currentSettings, +} from "discourse/tests/helpers/site-settings"; +import { getOwner, setDefaultOwner } from "discourse-common/lib/get-owner"; +import { setupURL, setupS3CDN } from "discourse-common/lib/get-url"; +import { createHelperContext } from "discourse-common/lib/helpers"; +import { buildResolver } from "discourse-common/resolver"; +import createPretender, { + pretenderHelpers, + applyDefaultHandlers, +} from "discourse/tests/helpers/create-pretender"; +import { flushMap } from "discourse/models/store"; +import { ScrollingDOMMethods } from "discourse/mixins/scrolling"; +import DiscourseURL from "discourse/lib/url"; +import { + resetSite, + applyPretender, +} from "discourse/tests/helpers/qunit-helpers"; +import PreloadStore from "discourse/lib/preload-store"; +import User from "discourse/models/user"; +import Session from "discourse/models/session"; +import { clearAppEventsCache } from "discourse/services/app-events"; +import QUnit from "qunit"; +import MessageBus from "message-bus-client"; +import deprecated from "discourse-common/lib/deprecated"; + +export default function setupTests(App) { + window.setResolver(buildResolver("discourse").create({ namespace: App })); + + sinon.config = { + injectIntoThis: false, + injectInto: null, + properties: ["spy", "stub", "mock", "clock", "sandbox"], + useFakeTimers: true, + useFakeServer: false, + }; + + // Stop the message bus so we don't get ajax calls + MessageBus.stop(); + + document.write( + '