unit-entry.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {EventEmitter, FakePerformance, FakePrefs, GlobalOverrider} from "test/unit/utils";
  2. import Adapter from "enzyme-adapter-react-16";
  3. import {chaiAssertions} from "test/schemas/pings";
  4. import chaiJsonSchema from "chai-json-schema";
  5. import enzyme from "enzyme";
  6. enzyme.configure({adapter: new Adapter()});
  7. // Cause React warnings to make tests that trigger them fail
  8. const origConsoleError = console.error; // eslint-disable-line no-console
  9. console.error = function(msg, ...args) { // eslint-disable-line no-console
  10. // eslint-disable-next-line no-console
  11. origConsoleError.apply(console, [msg, ...args]);
  12. if (/(Invalid prop|Failed prop type|Check the render method|React Intl)/.test(msg)) {
  13. throw new Error(msg);
  14. }
  15. };
  16. const req = require.context(".", true, /\.test\.jsx?$/);
  17. const files = req.keys();
  18. // This exposes sinon assertions to chai.assert
  19. sinon.assert.expose(assert, {prefix: ""});
  20. chai.use(chaiAssertions);
  21. chai.use(chaiJsonSchema);
  22. const overrider = new GlobalOverrider();
  23. const TEST_GLOBAL = {
  24. AddonManager: {
  25. getActiveAddons() {
  26. return Promise.resolve({addons: [], fullData: false});
  27. },
  28. },
  29. AppConstants: {MOZILLA_OFFICIAL: true},
  30. UpdateUtils: {getUpdateChannel() {}},
  31. BrowserWindowTracker: {getTopWindow() {}},
  32. ChromeUtils: {
  33. defineModuleGetter() {},
  34. generateQI() { return {}; },
  35. import() { return global; },
  36. },
  37. ClientEnvironment: {
  38. get userId() { return "foo123"; },
  39. },
  40. Components: {isSuccessCode: () => true},
  41. // eslint-disable-next-line object-shorthand
  42. ContentSearchUIController: function() {}, // NB: This is a function/constructor
  43. Cc: {
  44. "@mozilla.org/browser/nav-bookmarks-service;1": {
  45. addObserver() {},
  46. getService() {
  47. return this;
  48. },
  49. removeObserver() {},
  50. SOURCES: {},
  51. TYPE_BOOKMARK: {},
  52. },
  53. "@mozilla.org/browser/nav-history-service;1": {
  54. addObserver() {},
  55. executeQuery() {},
  56. getNewQuery() {},
  57. getNewQueryOptions() {},
  58. getService() {
  59. return this;
  60. },
  61. insert() {},
  62. markPageAsTyped() {},
  63. removeObserver() {},
  64. },
  65. "@mozilla.org/io/string-input-stream;1": {
  66. createInstance() {
  67. return {};
  68. },
  69. },
  70. "@mozilla.org/security/hash;1": {
  71. createInstance() {
  72. return {
  73. init() {},
  74. updateFromStream() {},
  75. finish() {
  76. return "0";
  77. },
  78. };
  79. },
  80. },
  81. "@mozilla.org/updates/update-checker;1": {createInstance() {}},
  82. },
  83. Ci: {
  84. nsICryptoHash: {},
  85. nsIHttpChannel: {REFERRER_POLICY_UNSAFE_URL: 5},
  86. nsITimer: {TYPE_ONE_SHOT: 1},
  87. nsIWebProgressListener: {LOCATION_CHANGE_SAME_DOCUMENT: 1},
  88. nsIDOMWindow: Object,
  89. },
  90. Cu: {
  91. importGlobalProperties() {},
  92. now: () => window.performance.now(),
  93. reportError() {},
  94. },
  95. dump() {},
  96. fetch() {},
  97. // eslint-disable-next-line object-shorthand
  98. Image: function() {}, // NB: This is a function/constructor
  99. NewTabUtils: {
  100. activityStreamProvider: {
  101. getTopFrecentSites: () => [],
  102. executePlacesQuery: async (sql, options) => ({sql, options}),
  103. },
  104. },
  105. OS: {
  106. File: {
  107. writeAtomic() {},
  108. makeDir() {},
  109. stat() {},
  110. exists() {},
  111. remove() {},
  112. removeEmptyDir() {},
  113. },
  114. Path: {
  115. join() {
  116. return "/";
  117. },
  118. },
  119. Constants: {
  120. Path: {
  121. localProfileDir: "/",
  122. },
  123. },
  124. },
  125. PlacesUtils: {
  126. get bookmarks() {
  127. return TEST_GLOBAL.Cc["@mozilla.org/browser/nav-bookmarks-service;1"];
  128. },
  129. get history() {
  130. return TEST_GLOBAL.Cc["@mozilla.org/browser/nav-history-service;1"];
  131. },
  132. observers: {
  133. addListener() {},
  134. removeListener() {},
  135. },
  136. },
  137. PluralForm: {get() {}},
  138. Preferences: FakePrefs,
  139. PrivateBrowsingUtils: {isWindowPrivate: () => false},
  140. DownloadsViewUI: {
  141. getDisplayName: () => "filename.ext",
  142. getSizeWithUnits: () => "1.5 MB",
  143. },
  144. FileUtils: {
  145. // eslint-disable-next-line object-shorthand
  146. File: function() {}, // NB: This is a function/constructor
  147. },
  148. Services: {
  149. dirsvc: {
  150. get: () => ({parent: {parent: {path: "appPath"}}}),
  151. },
  152. locale: {
  153. get appLocaleAsLangTag() { return "en-US"; },
  154. negotiateLanguages() {},
  155. },
  156. urlFormatter: {formatURL: str => str, formatURLPref: str => str},
  157. mm: {
  158. addMessageListener: (msg, cb) => cb(),
  159. removeMessageListener() {},
  160. },
  161. appShell: {hiddenDOMWindow: {performance: new FakePerformance()}},
  162. obs: {
  163. addObserver() {},
  164. removeObserver() {},
  165. },
  166. telemetry: {
  167. setEventRecordingEnabled: () => {},
  168. recordEvent: eventDetails => {},
  169. },
  170. console: {logStringMessage: () => {}},
  171. prefs: {
  172. addObserver() {},
  173. prefHasUserValue() {},
  174. removeObserver() {},
  175. getPrefType() {},
  176. clearUserPref() {},
  177. getChildList() { return []; },
  178. getStringPref() {},
  179. setStringPref() {},
  180. getIntPref() {},
  181. getBoolPref() {},
  182. getCharPref() {},
  183. setBoolPref() {},
  184. setIntPref() {},
  185. getBranch() {},
  186. PREF_BOOL: "boolean",
  187. PREF_INT: "integer",
  188. PREF_STRING: "string",
  189. getDefaultBranch() {
  190. return {
  191. setBoolPref() {},
  192. setIntPref() {},
  193. setStringPref() {},
  194. clearUserPref() {},
  195. };
  196. },
  197. },
  198. tm: {
  199. dispatchToMainThread: cb => cb(),
  200. idleDispatchToMainThread: cb => cb(),
  201. },
  202. eTLD: {
  203. getBaseDomain({spec}) { return spec.match(/\/([^/]+)/)[1]; },
  204. getPublicSuffix() {},
  205. },
  206. io: {
  207. newURI: spec => ({
  208. mutate: () => ({
  209. setRef: ref => ({
  210. finalize: () => ({
  211. ref,
  212. spec,
  213. }),
  214. }),
  215. }),
  216. spec,
  217. }),
  218. },
  219. search: {
  220. init() { return Promise.resolve(); },
  221. getVisibleEngines: () => Promise.resolve([{identifier: "google"}, {identifier: "bing"}]),
  222. defaultEngine: {
  223. identifier: "google",
  224. searchForm: "https://www.google.com/search?q=&ie=utf-8&oe=utf-8&client=firefox-b",
  225. wrappedJSObject: {
  226. __internalAliases: ["@google"],
  227. },
  228. },
  229. currentEngine: {identifier: "google", searchForm: "https://www.google.com/search?q=&ie=utf-8&oe=utf-8&client=firefox-b"},
  230. },
  231. scriptSecurityManager: {
  232. createNullPrincipal() {},
  233. getSystemPrincipal() {},
  234. },
  235. wm: {getMostRecentWindow: () => window, getEnumerator: () => []},
  236. ww: {registerNotification() {}, unregisterNotification() {}},
  237. appinfo: {appBuildID: "20180710100040"},
  238. },
  239. XPCOMUtils: {
  240. defineLazyGetter(object, name, f) {
  241. if (object && name) {
  242. object[name] = f();
  243. } else {
  244. f();
  245. }
  246. },
  247. defineLazyGlobalGetters() {},
  248. defineLazyModuleGetter() {},
  249. defineLazyModuleGetters() {},
  250. defineLazyServiceGetter() {},
  251. defineLazyServiceGetters() {},
  252. generateQI() { return {}; },
  253. },
  254. EventEmitter,
  255. ShellService: {isDefaultBrowser: () => true},
  256. FilterExpressions: {eval() { return Promise.resolve(false); }},
  257. RemoteSettings(name) {
  258. return {
  259. get() {
  260. if (name === "attachment") {
  261. return Promise.resolve([{attachment: {}}]);
  262. }
  263. return Promise.resolve([]);
  264. },
  265. on() {},
  266. };
  267. },
  268. Localization: class {
  269. async formatMessages(stringsIds) {
  270. return Promise.resolve(stringsIds.map(({id, args}) => ({value: {string_id: id, args}})));
  271. }
  272. },
  273. FxAccountsConfig: {
  274. promiseEmailFirstURI(id) {
  275. return Promise.resolve(id);
  276. },
  277. },
  278. TelemetryEnvironment: {
  279. setExperimentActive() {},
  280. },
  281. Sampling: {
  282. ratioSample(seed, ratios) {
  283. return Promise.resolve(0);
  284. },
  285. },
  286. };
  287. overrider.set(TEST_GLOBAL);
  288. describe("activity-stream", () => {
  289. after(() => overrider.restore());
  290. files.forEach(file => req(file));
  291. });